WebMock.disable_net_connect! does not work

I am trying to write a test case based on WebMock

to emulate an http API call. For this I have included webmock/rspec

in my file spec_helper.rb

and also added WebMock.disable_net_connect!(allow_localhost: true)

to deny HTTP requests over the internet. But when I run a dummy test to check the weather, http requests are blocked, I can see that http requests are still made.

Spec_helper.rb file:

ENV["RAILS_ENV"] ||= 'test'
require 'rubygems'
require File.expand_path("../../config/environment", __FILE__)
require 'authlogic/test_case'
include Authlogic::TestCase
require 'rspec/rails'
require 'rspec/autorun'
require 'rspec/mocks'
require 'capybara/rspec'
require 'capybara/rails'
require "paperclip/matchers"
require 'vcr'
require 'webmock/rspec'
WebMock.disable_net_connect!
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.mock_with :rspec
  config.use_transactional_fixtures = false

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.include Paperclip::Shoulda::Matchers

  config.include FactoryGirl::Syntax::Methods

  config.infer_base_class_for_anonymous_controllers = false
  config.include Rails.application.routes.url_helpers
  config.include Capybara::DSL
  config.render_views
  config.filter_run focus: true
  config.run_all_when_everything_filtered = true

end
VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.allow_http_connections_when_no_cassette = true
end

ActiveSupport::Dependencies.clear

      

Also the dummy test file I wrote:

require 'spec_helper'  
describe 'External request' do
    it 'queries FactoryGirl contributors on GitHub' do
      uri = URI('https://api.github.com/repos/thoughtbot/factory_girl/contributors')

      response = Net::HTTP.get(uri)

      expect(response).to be_an_instance_of(String)
    end
  end

      

Please help me find out if I am missing some configurations or if there is something else I am doing.

+3


source to share


1 answer


A problem was found in the configuration of the VCR

following configuration:

c.allow_http_connections_when_no_cassette = true

      



converting it to false

solved the problem as VCR

configs were rewriting webmock

configs because I defined c.hook_into :webmock

.

+4


source







All Articles