存储一个http请求以返回带有Net :: HTTPResponse的HTTPartyResponse

tom*_*mmi 8 ruby rspec rspec2

我正在尝试编写一个rspec规范来测试我的逻辑能够处理具有状态代码401的特定Net :: HTTPResponse.当我使用HTTParty时,.get将返回一个HTTPartyResponse并且我将检索带有httparty_repsonse_object.response的Net :: HTTPResponse.

net_response_object = Net::HTTPResponse.new(1.1, 401, 'Forbidden')
#not sure what to do here? write a test double to return a net_response_object?
stub_request(:any, /hello.com/).to_return(a_http_party_response_object)
Run Code Online (Sandbox Code Playgroud)

tom*_*mmi 6

我发现这是正确的方法

it 'will test this' do
   stub_request(:any, /hello.com/).to_return(:status => [401, "Forbidden"])
   ...
end
Run Code Online (Sandbox Code Playgroud)

HTTParty应该解析模拟的http响应并返回HTTPartyResponse.


Tan*_*ili 5

您可以使用Webmock,它支持模拟 HTTParty 请求。