2012-06-24 63 views
6

我想写一个rspec spec来测试我的逻辑能够处理具有状态码401的特定Net :: HTTPResponse。 m使用HTTParty,.get将返回一个HTTPartyResponse,我将通过httparty_repsonse_object.response检索Net :: HTTPResponse。Stub一个HTTP请求返回一个HTTP :: HTTPResponse与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) 

回答

4

我想通了,这是应该解析嘲笑HTTP响应并返回我HTTPartyResponse正确的方法

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

HTTParty。

4

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