Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

including webmock/rspec in any spec file results in all specs having web disabled #895

Open
thoraxe opened this issue Jun 19, 2020 · 4 comments

Comments

@thoraxe
Copy link

thoraxe commented Jun 19, 2020

Given a file spec/services/foo_spec.rb with:

require 'rails_helper'
require 'spec_helper'
require 'webmock/rspec'

if I simply run bundle exec rspec to execute all tests, I get the following error from a completely different spec file (spec/controllers/admin/data_source_controller_spec.rb):

 2) Admin::DataSourcesController DELETE #destroy redirects to the field
     Failure/Error: body, ok=  Rack::Superfeedr.retrieve_by_topic_url(self.url, {format: 'json'})
     
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled

I tried moving require webmock/rspec to both spec_helper and rails_helper immediately followed by a WebMock.disable! but that had no effect -- webmock was still enabled everywhere.

I'm assuming this is user error but I'm not understanding how I can use webmock only for certain spec files.

Also, moving the require webmock/rspec into a specific test block fixed the problem in other specs (they didn't have webmock enabled) but once the require is inside the RSpec.describe block it doesn't appear that webmock works (it didn't detect the stub I was testing for).

@thoraxe
Copy link
Author

thoraxe commented Jul 28, 2020

Is it intended/expected that require webmock/rspec anywhere (in any spec) results in it being loaded everywhere? I am thinking this is an artifact of the way that rspec loads all tests/specs ahead of time? This is probably not a webmock issue, I don't think?

@bblimke
Copy link
Owner

bblimke commented Sep 11, 2020

@thoraxe yes, it's intended that require webmock/rspec results with WebMock being loaded everywhere.

If you take a look at webmock/rspec source, it simply loads WebMock, enables it (globally) and adds some callback to rspec.

WebMock interecepts the requests on http client level. It doesn't know where the requests came from. Whether they came from a code invoked inside rspec test or from server code that was loaded as well and there is no way to control that.

I suggest you invoke disable_net_connect! and add allow option with the urls/hosts/ports that you don't wan't to be intercepted by WebMock.

@cecilian
Copy link

Hello,
I have a similar problem but for some reason the suggested solution is not working for me, I still get WebMock::NetConnectNotAllowedError for the allowed urls.

I tried putting the line

allowed_sites = [
  "https://chromedriver.storage.googleapis.com",
  "https://github.com/mozilla/geckodriver/releases",
  "https://selenium-release.storage.googleapis.com",
   "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver"
]
WebMock.disable_net_connect!(allow_localhost: true, allow: allowed_sites)
  1. directly in the RSpec.configure block in rails_helper.rb
  2. in a before(:each) block inside the Rspec.configure one
  3. directly inside the test where I need the stub

In all cases the requests to the above url are blocked.
What is most puzzling is that I have the same setup on a different project where it is working fine (i.e., allowed urls are allowed as expected).
Any hint on how to debug where I'm going wrong?

@bblimke
Copy link
Owner

bblimke commented Nov 3, 2021

@cecilian Is the http request made from the code invoked inside the test? If yes, then perhaps there is some code that again disables the connections after the setup and before the request is invoked?

The following code works find:

WebMock.enable!

allowed_sites = [
  "https://chromedriver.storage.googleapis.com",
  "https://github.com/mozilla/geckodriver/releases",
  "https://selenium-release.storage.googleapis.com",
   "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver"
]
WebMock.disable_net_connect!(allow_localhost: true, allow: allowed_sites)

Net::HTTP.get(URI.parse("https://chromedriver.storage.googleapis.com"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants