我正在尝试编写一个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)
我发现这是正确的方法
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.