Search code examples
ruby-on-rails-3rspecrspec2rspec-rails

Rspec for REST API call


Im trying to implement rspec for a http call... But I have not succeeded so far..

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 

I would need an rspec for this..Though Im new to this, I tried many ways but it says expected: 1 time received: 0 times

Here is all what I have written :

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

Please correct me if Im wrong.. Any help is appreciated! Thanks


Solution

  • Of course you have to activate the method in which the new statement is called so your code should look something like:

    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