I’m trying to test a method I have on one of my rails models. I am returning the HTTP status from a url and don’t know how I can stub the return to test out different return codes to make sure my code works for different situations.
Here’s the line of code I want to mock:
response = Net::HTTP.get_response(URI.parse(self.url))
I want to have the Net:HTTP.get_response to return a specific HTTPResponse for each of the tests I have in my spec.
describe Site do
before :each do
FactoryGirl.build :site
end
context "checking site status" do
it "should be true when 200" do
c = FactoryGirl.build :site, url:"http://www.example.com/value.html"
#something to mock the Net::HTTP.get_response to return and instance of Net::HTTPOK
c.ping.should == true
end
it "should be false when 404" do
c = FactoryGirl.build :site, url:"http://www.example.com/value.html"
#something to mock the Net::HTTP.get_response to return and instance of Net::HTTPNotFound
c.ping.should == false
end
end
end
How would I stub out the return values from get_response?
I’d recommend fakeweb for this, e.g.: