用于REST API调用的Rspec

时间:2013-01-08 13:11:45

标签: ruby-on-rails-3 rspec rspec2 rspec-rails

我试图为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 

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

1 个答案:

答案 0 :(得分:0)

当然,您必须激活调用新语句的方法,以便您的代码看起来像:

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