Rails-如何为特定/伪造的http或ajax响应设置特定的响应时间 [英] Rails - How to stub a certain response time for a stubbed / fake http or ajax response
问题描述
我想测试一个功能,当Rails UJS / ajax超时时,我的应用程序会向用户发送自定义消息。
I want to test a feature that in my app sends the usera custom message when a Rails UJS/ajax times-out.
使Rails UJS超时的代码请求本身就在应用程序上:
The code to timeout the Rails UJS request is itself on the app:
$.rails.ajax = function(options) {
if (!options.timeout) {
options.timeout = 10000;
}
return $.ajax(options);
};
在chrome开发工具上进行观察时,在本地开发模式下超时会发生什么情况,代码状态奇怪地是 200 ,但是当它超时时,确实向用户显示了我的消息。
When observing on chrome dev tools what happened when it timed out on my local dev mode, I notice the code status is strangely 200 but as it "times out", my message is indeed displayed to the user.
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"
});
}
}
});
以下是我当前的测试(使用喘气的钞票宝石)。我设法为我的Ajax请求存入http响应,但是我不知道如何告诉rspec等待和超时,例如花费11秒,但仍未发送任何响应给xhr调用:) (xhr的最大超时设置为高于10000毫秒,因此10 sec <11sec,它应该在rspec测试中超时)。
Below is my current test (using puffing bill gem). I managed to stub the http response for my ajax request but I don't know how to tell rspec to "wait" and timeout like take 11 seconds and still not send any response to the xhr call:) (xhr max timeout is set to 10 000 milliseconds above so 10 sec<11sec, it should time out inside the rspec test)
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版本,并且只要您希望请求接受就可以睡觉
If you really want it to just wait for the timeout I believe you can use the Proc version of and_return and sleep for as long as you want the request to take
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只是传递您希望元素在内
调用内出现的时间
also - rather than wait_for_ajax just pass the amount of time you expect the element to take to appear to the within
call
within('ul.messenger', wait: 11) do ...
这篇关于Rails-如何为特定/伪造的http或ajax响应设置特定的响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!