I have a controller action that I'm trying to test that downloads a file.
def download_file(url)
response = Net::HTTP.get_response(URI(url))
if response.code == '200'
return response.body
end
end
In reading up on rspec I see that I can create a double of an object and stub it's methods. But, I can't seem to figure out how to mock it's properties. So for this test, how do I mock the response that is returned from the get_response call to have a code and body property? I've tried this but it's not working. Instead I'm getting back nil.
it 'calls Net::HTTP.get_response which returns a response' do
@netresponse = double("response", {:body => 'this is a test', :code => 200})
@url = 'http://www.google.com'
allow(Net::HTTP).to receive(:get_response).and_return(@netresponse)
return_value = controller.download_file(@url)
expect(return_value).to eq('this is a test')
end
You're stubbing code
with an integer, but the implementation compares against a string.