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

Allow global stubbing (e.g. before :suite) #484

Open
collimarco opened this issue May 28, 2015 · 12 comments
Open

Allow global stubbing (e.g. before :suite) #484

collimarco opened this issue May 28, 2015 · 12 comments

Comments

@collimarco
Copy link

In Rspec this works:

RSpec.configure do |config|
  config.before :each do
     WebMock.stub_request(...)
  end
end

However this doesn't work:

RSpec.configure do |config|
  config.before :suite do
     WebMock.stub_request(...)
  end
end

I think it would be useful to stub something globally only once in a before :suite.

@VincentZhao
Copy link
Contributor

Because it calls WebMock.reset! in after(:each) block, if you require 'webmock/rspec'.
See https://github.com/bblimke/webmock/blob/master/lib/webmock/rspec.rb#L29.

@kalinchuk
Copy link

+1

@giovannibenussi
Copy link

@VincentZhao do you know why is necessary to reset the mocks after every test (as the source code does)?

@bblimke
Copy link
Owner

bblimke commented Sep 13, 2017

@giovannibenussi because we want to have a clean state when entering a new example.

@Epigene
Copy link

Epigene commented Oct 13, 2017

There should be an option to stub globally by disabling WebMock.reset! conditionally.
That way the preferred "each example in a clean state with its own setup" approach is the default, but can be overridden if necessary.

@bblimke
Copy link
Owner

bblimke commented Oct 13, 2017

@Epigene Do you have any suggestion how that option to disable reset conditionally could work?

btw. you don't have to include webmock/rspec config. You can define your own config.

@Epigene
Copy link

Epigene commented Oct 15, 2017

Now that I think about it, the functionality could work like this:

  1. Allow stubs to have optional metadata (maybe they already support something like that),
  2. Have a before(:all) call that sets up stubs marked as "global" WebMock.stub_request(metadata: {type: "global"}),
  3. Do not require 'webmock/rspec', instead roll your own reset with after(:each) { WebMock.reset!(except: {metadata: "global"}) } to explicitly keep the "global" mocks.
  4. Allow WebMock.reset! method to receive optional argument for reset exclusions,
  5. Allow WebMock.reset! method to receive optional argument for reset exclusions with a special :all argument to skip resetting completely.

5th point would be a quick-and-dirty way to skip reset entirely. If last call of WebMock.reset! dictates the behavior, then projects could have this kind of setup:

# in rails_helper.rb
after do
  WebMock.reset!
end

# in some spec file
describe "long setup" do
  after do
    WebMock.reset!(except: :all)
  end
end

@pacop
Copy link

pacop commented Dec 11, 2018

some news right here? Is this possible? Thanks

@krtschmr
Copy link

krtschmr commented Aug 6, 2019

yeah, we need this. for sure!

i don't wanna stub the same and same request over and over again?

@mejiaej
Copy link

mejiaej commented Jun 24, 2021

Has anybody found a workaround and be able to stub things once?

@jlurena
Copy link

jlurena commented Oct 21, 2021

@mejiaej https://www.rubydoc.info/github/bblimke/webmock/WebMock#globally_stub_request-class_method

Thanks! Not the ideal scenario (I don't want to stub on every example) but this is what works so far.

RSpec.configure do |config|
  config.before :each do
    WebMock.globally_stub_request { |request|
      if request.uri.to_s =~ /my_url/
        { status: 200, body: '{}', headers: {} }
      end
    }
  end
end

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