bblimke / webmock

Library for stubbing and setting expectations on HTTP requests in Ruby.
MIT License
3.96k stars 556 forks source link

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

Open collimarco opened 9 years ago

collimarco commented 9 years ago

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 commented 9 years ago

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 commented 7 years ago

+1

giovannibenussi commented 7 years ago

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

bblimke commented 7 years ago

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

Epigene commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 5 years ago

some news right here? Is this possible? Thanks

krtschmr commented 5 years ago

yeah, we need this. for sure!

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

mejiaej commented 3 years ago

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

kirylrb commented 3 years ago

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

jlurena commented 2 years ago

@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