We are using Capybara and Cucumber with Webmock. We are running to a problem that an asychronous HTTP request happens after Webmock has cleared the stubs, so intermittently we get the warning to implement #stub_request on something that we had already stubbed.
This happens frequently for us because we make an HTTP request to our metrics provider upon ever server request.
We saw that VCR used an undocumented feature of Webmock that supports global stubs. Would you accept a pull request for a method called #stub_request_globally.
def stub_request_globally(method, uri)
WebMock::StubRegistry.instance.global_stubs << WebMock::RequestStub.new(method, uri)
@jtarchie and @tracedwax
Global stubs were added just for the purpose of VCR but they are a workaround rather than desired part of the API.
If your problem is that WebMock by default clears stubs then why don't you override the default behaviour.
I assume you require 'webmock/cucumber' right? It's very simple https://github.com/bblimke/webmock/blob/master/lib/webmock/cucumber.rb. Instead of requiring it, just define your own behaviour,
and set when WebMock stubs should be cleared.
We could do this, but for any new web stubs that are added, we will want to clear them by default. What you're saying makes a lot of sense, it's most desirable to have the web stubs local to each test.
We're just looking for a way to solve this particular problem, which could happen to anyone that has any API calls that happen on lingering Ajax requests in cucumber features or feature specs. Cucumber will exit and unstub once the last step is complete, but there's then a race condition which will frequently trigger the server hit and external web API call.
What might you suggest for how to avoid that other than using a global web stub for just the API calls that happen on every server request?
Otherwise, the global web stubs used only for these instances are working well for us now, so we'll continue to use that undocumented feature for as long as it's supported.
Thanks for getting back to us so quickly!