Rails - 如何为存根/假 http 或 ajax 响应存根特定响应时间
Rails - How to stub a certain response time for a stubbed / fake http or ajax response
我想测试一个功能,当 Rails UJS/ajax 超时时,我的应用程序会向用户发送自定义消息。
使 Rails UJS 请求超时的代码本身就在应用程序中:
$.rails.ajax = function(options) {
if (!options.timeout) {
options.timeout = 10000;
}
return $.ajax(options);
};
当观察 chrome 开发工具在我的本地开发模式超时时发生了什么,我注意到代码状态很奇怪 200 但是 "times out",我的消息确实显示给了用户
on('ajax:error',function(event,xhr, status, error){
// display message in modal for users
if(status == "timeout") {
console.log( ' ajax request timed out');
var msg;
msg = Messenger().post({
hideAfter: 8,
message: "Too long, the app timed out"
});
}
}
});
下面是我当前的测试(使用 puffing bill gem)。我设法存根了我的 ajax 请求的 http 响应,但是 我不知道如何告诉 rspec 到 "wait" 和超时 就像 take 11秒,仍然没有对 xhr 调用发送任何响应:)(xhr 最大超时设置为 10000 毫秒以上,所以 10 秒<11 秒,它应该在 rspec 测试中超时)
it " displays correct modal message appears correctly when xhr call timeout" do
visit deal_page_path(deal)
proxy.stub("http://127.0.0.1:59533/deals/dealname/ajaxrequest").and_return(:code => 200)
first('a.button').click
wait_for_ajax
within('ul.messenger') do
expect(page).to have_content('Too long, the app timed out')
end
end
如果你真的想让它等待超时,我相信你可以使用 and_return 的 Proc 版本,只要你想让请求接受就可以休眠
proxy.stub("http://127.0.0.1:59533/deals/dealname/ajaxrequest").and_return(
Proc.new { |params, headers, body|
sleep 11
{code: 200}
} )
另外 - 而不是 wait_for_ajax 只是传递您期望元素出现在 within
调用中所花费的时间
within('ul.messenger', wait: 11) do ...
我想测试一个功能,当 Rails UJS/ajax 超时时,我的应用程序会向用户发送自定义消息。
使 Rails UJS 请求超时的代码本身就在应用程序中:
$.rails.ajax = function(options) {
if (!options.timeout) {
options.timeout = 10000;
}
return $.ajax(options);
};
当观察 chrome 开发工具在我的本地开发模式超时时发生了什么,我注意到代码状态很奇怪 200 但是 "times out",我的消息确实显示给了用户
on('ajax:error',function(event,xhr, status, error){
// display message in modal for users
if(status == "timeout") {
console.log( ' ajax request timed out');
var msg;
msg = Messenger().post({
hideAfter: 8,
message: "Too long, the app timed out"
});
}
}
});
下面是我当前的测试(使用 puffing bill gem)。我设法存根了我的 ajax 请求的 http 响应,但是 我不知道如何告诉 rspec 到 "wait" 和超时 就像 take 11秒,仍然没有对 xhr 调用发送任何响应:)(xhr 最大超时设置为 10000 毫秒以上,所以 10 秒<11 秒,它应该在 rspec 测试中超时)
it " displays correct modal message appears correctly when xhr call timeout" do
visit deal_page_path(deal)
proxy.stub("http://127.0.0.1:59533/deals/dealname/ajaxrequest").and_return(:code => 200)
first('a.button').click
wait_for_ajax
within('ul.messenger') do
expect(page).to have_content('Too long, the app timed out')
end
end
如果你真的想让它等待超时,我相信你可以使用 and_return 的 Proc 版本,只要你想让请求接受就可以休眠
proxy.stub("http://127.0.0.1:59533/deals/dealname/ajaxrequest").and_return(
Proc.new { |params, headers, body|
sleep 11
{code: 200}
} )
另外 - 而不是 wait_for_ajax 只是传递您期望元素出现在 within
调用中所花费的时间
within('ul.messenger', wait: 11) do ...