我正在尝试在我的某个导轨模型上测试我的方法。我从URL返回HTTP状态,并且不知道如何存根返回以测试不同的返回代码以确保我的代码适用于不同的情况。
这是我要模拟的代码行:
response = Net::HTTP.get_response(URI.parse(self.url))
我想让Net:HTTP.get_response
为我的规范中的每个测试返回一个特定的HTTPResponse。
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
如何从get_response中隐藏返回值?
答案 0 :(得分:3)
我建议fakeweb为此,例如:
FakeWeb.register_uri(:get,
"http://example.com/value.html",
:body => "Success!")
FakeWeb.register_uri(:get,
"http://example.com/value.html",
:body => "Not found!",
:status => ["404", "Not Found"])