我正在尝试在我的一个rails模型上测试一个方法。我从一个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中删除返回值?
最佳答案
为此,我建议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"])
关于ruby-on-rails - 测试网:: HTTP.get_Response(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12417018/