简体   繁体   English

RSpec中的模拟请求超时

[英]Mocking request timeout in RSpec

I have a class that makes a request to another service and I want to test error handing around that request. 我有一个向其他服务发出请求的类,我想测试处理该请求的错误。

The code looks like this: 代码如下:

  response = RequestObject.post #this triggers a HTTP request

  LocalObject.update(foreign_id: response.id, retrieved_at: Time.current )

In my tests I stub the request out and return JSON on success, but I want to test that if that response raises an error that the :foreign_id and :retrieved_at aren't set. 在我的测试中,我对请求进行存根处理,并在成功时返回JSON,但我想测试是否该响应引发未设置:foreign_id:retrieved_at的错误。

I stub the successful request like this: 我将成功的请求存根如下:

allow(RequestObject).to receive(:post).and_return({id: 1,})

What's a good way to mock RequestObject raising a timeout error? 模拟RequestObject引发超时错误的好方法是什么?

(I'm using the Faraday gem for my requests) (我正在根据要求使用法拉第宝石

You can use #and_raise to mock an error. 您可以使用#and_raise模拟错误。

allow(RequestObject).to receive(:post).and_raise(SomeError)

That should allow you to test that code path. 那应该允许您测试该代码路径。

Here 'sa link to the rspec docs. 这里是指向rspec文档的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM