You can try this:
original_get_response = Net::HTTP.method(:get_response)
Net::HTTP.stub(:get_response) do |uri|
(uri.to_s == 'http://google.de') ? FakeResponse.new : original_get_response.call(uri)
end
Looking at the discussions around the addition of and_call_original
a year ago, it seems that storing and calling the original method like this is a pretty common (but awkward) idiom. (There may be an original_method
call available to you now but I don't have a build handy to test it out.)
Edit: later, I got to a build and tried simply using .with(URI('...'))
, which worked fine. Unless I've misunderstood the question?
require 'spec_helper'
require 'net/http'
describe :nethttp do
before :each do
Net::HTTP.stub(:get_response).and_return('something')
Net::HTTP.stub(:get_response).with(URI('http://www.google.com')).and_return('something else')
end
it { Net::HTTP.get_response(URI('http://www.google.com')).should eql 'something else' }
it { Net::HTTP.get_response(URI('http://www.google.co.uk')).should eql 'something' }
end