Search code examples
ruby-on-railsrubyrspecwebmock

RSpec and WebMock: ignore request


When I run the test then I get this an error:

/gems/ruby-2.3.1@app2/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb:166:in `block in <class:TyphoeusAdapter>': Real HTTP connections are disabled. Unregistered request: HEAD https://api.travis-ci.org/ with headers {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'} (WebMock::NetConnectNotAllowedError)

You can stub this request with the following snippet:

stub_request(:head, "https://api.travis-ci.org/").
  with(:headers => {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'}).
  to_return(:status => 200, :body => "", :headers => {})

That's what I did to solve this problem:

spec_helper.rb:

require 'webmock/rspec'
require 'capybara/rspec'
require 'factory_girl_rails'

WebMock.disable_net_connect!(allow_localhost: true)
RSpec.configure do |config|
  config.before(:each) do
    stub_request(:any, "https://api.travis-ci.org/")
      .with(:headers => {'Accept'=>'application/vnd.travis-ci.2+json', 'User-Agent'=>'Travis/1.8.2 (Mac OS X 10.11.4 like Darwin; Ruby 2.3.1-p112; RubyGems 2.5.1) Faraday/0.9.2 Typhoeus/0.8.0'})
      .to_return(:status => 200, :body => "", :headers => {})
  end
end

But I still continue to get this error, maybe someone have ideas how to fix it?

UPD: I want to write spec for api requests using webmock gem. But I don't need requests from travis-ci and want to add these requests to "ignore".


Solution

  • It solved my problem:

    WebMock.disable_net_connect!(:allow => 'api.travis-ci.org')