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

requiring webmock/rspec after webmock somehow breaks tests in rspec #534

Open
illegalnumbers opened this issue Oct 16, 2015 · 3 comments
Open

Comments

@illegalnumbers
Copy link

So I have a setup using RSpec. Webmock is required in our spec helper and we recently tried using webmock/rspec as well. After doing a require for webmock/rspec though our test suite is broken for several tests that use webmock to rack requests to a fake object representing an api. It seems they go out and make requests to the actual api instead of our fake. This is very strange behavior since we have a before block that sets net connections to be disabled.

Examples of what I mean

before :all do
    WebMock.disable_net_connect!(allow_localhost: true)
    WebMock.stub_request(:any, /rest.developer.service.com/).to_rack(Fakes::ServiceFake)
    Service::Base.new.login_app # our call to the service
  end

Works fine when in our rails_helper we just have

require 'webmock'

breaks when we do

require 'webmock'
require 'webmock/rspec'
@bblimke
Copy link
Owner

bblimke commented Oct 16, 2015

Please have a look at https://github.com/bblimke/webmock/blob/master/lib/webmock/rspec.rb

WebMock resets all stubs before each example.

Therefore your before :all which is set up once before all examples is not going to work as webmock will destroy that state after first example.

Either use before :each instead of before :all or don't include webmock/rspec but prepare your own config.

@illegalnumbers
Copy link
Author

Ok, that makes sense. Is this not documented anywhere that doing the normal require vs the require rspec does that reset? It was a bit jarring having the library work as expected one way and suddenly different under another. Though I do appreciate what's happening under the hood and understand why it's there. I may just have a selective memory if it is documented. The problem we had was just wanting to use all the great RSpec matchers from Webmock; not that we wanted to use the config or setup.

@bblimke
Copy link
Owner

bblimke commented Oct 20, 2015

@bytenel I try to avoid before :all as it often leads to issues, not only with WebMock.

Do you have a suggestion how should it be documented to make it clear and intuitive?

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

No branches or pull requests

3 participants