如何测试(rspec)耗时太长的http请求? [英] how do I test (rspec) a http request that takes too long?
问题描述
如果请求使用rspec花费的时间太长,该如何测试行为?
How do I test the behavior if a request takes too long with rspec?
我正在考虑使用线程来模拟:
I am thinking of using thread to mock this:
describe "Test" do
it "should timeout if the request takes too long" do
lambda {
thread1 = Thread.new { #net::http request to google.com }
thread2 = Thread.new { sleep(xx seconds) }
thread1.join
thread2.join
}.should raise_error
end
end
我想确保在首次发出请求之后,另一个线程插入",在这种情况下,该线程只是睡眠xx秒.那我应该期望请求超时,因为执行时间太长了
I want to make sure that after the request is first made, another thread "kicks in" which in this case is just a sleep for xx seconds. Then I should expect the request to timeout because it takes too long to execute
我认为有更好的方法可以做到这一点.考虑到我请求的网址无关紧要.我只是想测试一下,如果执行时间太长,确实会超时.
I think that there are better ways to do this. Given the fact that the url I am requesting is not relevant. I just want to test that it will indeed timeout if it takes too long to execute.
我可以使用stub(),expect()或任何rspec功能来模拟吗?
Can I use stub(), expect() or any rspec features to simulate this?
有什么方法可以将块"传递给存根方法
Is there any way that I can pass in a 'block' into stub method
http_request_to_google.stub(:connection).executethisblock(sleep for xx seconds)
.and_throw error ?
感谢您的帮助
推荐答案
如果您纯粹在乎Net :: HTTP引发Timeout :: Error的问题,您总是可以强制它使用模拟此处是可以与RSpec一起使用的各种东西的很好的编译.
If you purely care about Net::HTTP raising a Timeout::Error, you could always just force it to return the error with a mock, here is a good compilation of various things you can use with RSpec.
这取决于您的确切Net :: HTTP请求,但是诸如Net::HTTP.should_receive(:request_get).and_raise(Timeout::Error)
之类的内容将跳过任何网络调用,并立即引发错误.
It would depend on your exact Net::HTTP request, but something such as Net::HTTP.should_receive(:request_get).and_raise(Timeout::Error)
would skip any networking calls and just raise the error immediately.
这篇关于如何测试(rspec)耗时太长的http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!