Webmock Unregistered request blows up my test

3.4k views Asked by At

In my spec_helper, I have webmock setup like so:

WebMock.disable_net_connect!(allow_localhost: true)

RSpec.configure do |config|
  config.before(:each) do
    stub_request(:get, /cdn.\w+.io/).
      with(:headers => {'Accept-Encoding'=>'gzip', 'Access-Token'=> ENV['ACCESS_TOKEN'], 'Api-Key'=>ENV['API_KEY'], 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
      to_return(:status => 200, :body => "", :headers => {})
  end
end

Now when I run this test individually using bundle exec rspec spec/stack_client_query_spec.rb, it passes.

  describe "can query for entries by asking for a content_type" do
    it "returns a Typhoeus response with queried with a content_type" do
      response = create_client.content_type(content_type_uid).get
      expect(response).to be_instance_of(Typhoeus::Response)
    end
  end

However when I run the full test suite using bundle exec rspec, the same test blows up with this output:

Failure/Error:
       response = Typhoeus::Request.new(
         endpoint,
         headers: { api_key: headers[:api_key], access_token: headers[:access_token],
         accept_encoding: "gzip" }
       ).run

     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://cdn.mycustom.domain/v3/content_types/shirts/entries/ with headers {'Accept-Encoding'=>'gzip', 'Access-Token'=>'bltbdc42da30987971c', 'Api-Key'=>'blt1c501b5fa4b64377', 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}

       You can stub this request with the following snippet:

       stub_request(:get, "http://cdn.mycustom.domain/v3/content_types/shirts/entries/").
         with(:headers => {'Accept-Encoding'=>'gzip', 'Access-Token'=>'bltbdc42da30987971c', 'Api-Key'=>'blt1c501b5fa4b64377', 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:get, "/cdn.\w+.io/").
         with(:headers => {'Accept-Encoding'=>'gzip', 'Access-Token'=>'bltbdc42da30987971c', 'Api-Key'=>'blt1c501b5fa4b64377', 'Expect'=>'', 'User-Agent'=>'Typhoeus - https://github.com/typhoeus/typhoeus'})

It seems like an earlier test is overwriting the request domain or something. I am not sure why!? What am I doing wrong? How do I fix this?

1

There are 1 answers

0
Giridhar Bandi On BEST ANSWER

Looks like you have stubbed

stub_request(:get, /cdn.\w+.io/)

but the actual request is going to

http://cdn.mycustom.domain/

you can change your stub to

stub_request(:get, /cdn.\w+.domain/)