0

我正在尝试为 http 调用实现 rspec……但到目前为止我还没有成功……

http = Net::HTTP.new("secured site url", "443")
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE  
response = http.get("/query params",initheader = {'Accept' =>'application/xml'})
return response 

我需要一个 rspec 。虽然我是新手,但我尝试了很多方法,但它说预期:1 次收到:0 次

这是我写的所有内容:

it "should be success" do
  @mock_http = mock("http")
  @mock_http.should_receive(:new).with("secured site url")
end 

如果我错了,请纠正我..任何帮助表示赞赏!谢谢

4

1 回答 1

1

当然,您必须激活调用新语句的方法,这样您的代码应该类似于:

it "should be success" do
  @mock_http = mock("http")
  Net::HTTP.should_receive(:new).with("secured site url", "443")
  some_method
end

def some_method
  http = Net::HTTP.new("secured site url", "443")
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE  
  response = http.get("/query params",initheader = {'Accept' =>'application/xml'})
  return response 
end
于 2013-01-08T13:38:43.923 回答