2012-06-24 95 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請求。