Having issue with webmock in stubbing request

1.1k Views Asked by At

I am writin specs for my gem and I am using webmock to mock http requests. But i keep on getting this weird error.

Here is my specs code

require 'spec_helper'

describe 'Generator::Exotel' do

  describe '#success' do
    let(:resps) { {"Status"=>"200", "Message"=>"Success"} }

    before do
      stub_request(:post, "https://test_sid:[email protected]/v1/Accounts/#{Generator::configuration.sid}/Sms/send").
        with(:body => {:To => 1234, :Body => "test sms"}, :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
          to_return(:body => resps.to_json, :headers => {})
    end

    it 'returns response object for success' do
      response = Generator::Exotel.send(:to => 1234, :body => "test sms")
      expect(response.to_json).to eq (resps.to_json)
    end
  end  

  describe '#failure' do
    let(:resp) { {"Status"=>"401", "Message"=>"Not Authenticated"} }

    before do
      stub_request(:post, "https://test_sid:[email protected]/v1/Accounts/#{Generator::configuration.sid}/Sms/send").
        with(:body => {:To => 1234, :Body => "test sms"}, :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
          to_return(:body=> resp.to_json, :headers => {})
    end

    it 'returns response object for failure' do
      response = Generator::Exotel.send(:to => 1234, :body => "test sms")
      expect(response.to_json).to eq (resp.to_json)
    end
  end
end

Whenever i run rspec, i am getting this following error

Generator::Exotel #success returns response object for success
     Failure/Error: response = self.class.post("/#{Generator::configuration.sid}/Sms/send",  {:body => params, :basic_auth => auth })

     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: POST https://twilix.exotel.in/v1/Accounts/test_sid/Sms/send with body 'To=1234&Body=test%20sms' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Basic dGVzdF9zaWQ6dGVzdF90b2tlbg==', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:post, "https://twilix.exotel.in/v1/Accounts/test_sid/Sms/send").
         with(:body => "To=1234&Body=test%20sms",
              :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Basic dGVzdF9zaWQ6dGVzdF90b2tlbg==', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:post, "https://test_sid:[email protected]/v1/Accounts/test_sid/Sms/send").
         with(:body => {"Body"=>"test sms", "To"=>1234},
              :headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})

       ============================================================

I have googled a lot and found some solutions, i.e,

Solution 1

Relish Documentation

“WebMock::NetConnectNotAllowedError”

Also i had a look at this post , But in vain.

Also i tried using WebMock.disable_net_connect!(:allow_localhost => true) but got the same result. Anyone know what am i doing wrong? I am really new to ruby and for first time i am writing specs and its really confusing me.

1

There are 1 best solutions below

2
On

You cannot make external http requests with webmock, as rightly said by various posts that you mentioned. I notice your stubbed urls have interpolated variables in them ie "https://test_sid:[email protected]/v1/Accounts/#{Generator::configuration.sid}/Sms/send". You need to:

  1. Make sure Generator::configuration.sid is accessible within the specs
  2. If it's not accessible then just return a plain url which is perfectly fine