Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub a http request to return a HTTPartyResponse with a Net::HTTPResponse

Tags:

ruby

rspec

rspec2

I'm trying to write a rspec spec to test that my logic is able to handle a particular Net::HTTPResponse that has status code 401. As I'm using HTTParty, .get will return me a HTTPartyResponse and I'll retrieve the Net::HTTPResponse with the httparty_repsonse_object.response.

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)
like image 598
tommi Avatar asked Jun 24 '12 17:06

tommi


1 Answers

I figured out this is the correct way

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

HTTParty is supposed to parse the mocked http response and return me a HTTPartyResponse.

like image 157
tommi Avatar answered Nov 15 '22 17:11

tommi