【发布时间】:2016-06-11 15:20:14
【问题描述】:
我想要测试由 Rails UJS 发起的 AJAX 调用。具体来说,我使用 Rails UJS ajax 事件来提供错误情况。
我想测试它们,但我不知道如何告诉 rspec/capybara “存根”并假设错误代码
$("button").
on('ajax:error',function(event,xhr, status, error){
if(status == "timeout") {
var msg;
msg = Messenger().post({
message: "This is taking too long"
});
} else {
var msg;
msg = Messenger().post({
message: "it seems there is a bug. Please try again."
});
};
});
我想做如下的事情:
describe "test returning error message", js: true do
it "should be successful" do
visit deal_page_path(deal)
first('a.button').click
stub(:xhr.status) = "timeout"
expect(page).to have_content('This is taking too long')
end
end
如何做到这一点?
注意:ajax 请求是内部的,它们不会发送到第三方 API 或服务(例如 facebook for ex)。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec capybara ujs