2012-09-14 4 views
2

나는 내 레일 모델 중 하나에 대한 테스트 방법을 시도하고 있습니다. URL에서 HTTP 상태를 반환하고 다른 반환 코드를 테스트하여 리턴 코드를 다른 상황에 적용 할 수 있는지 알 수 없습니다.Testing Net :: HTTP.get_Response()

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에서 반환 값을 어떻게 내 보냅니다.

답변

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"])