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

Cached nil responses for missing stubs affect subsequent tests #1019

Open
toaster opened this issue Feb 16, 2023 · 2 comments
Open

Cached nil responses for missing stubs affect subsequent tests #1019

toaster opened this issue Feb 16, 2023 · 2 comments

Comments

@toaster
Copy link

toaster commented Feb 16, 2023

Given the following RSpec test:

require "httpclient"
require "webmock"
require "webmock/rspec/matchers"

RSpec.describe "webmock issue" do
  before(:all) do
    WebMock.enable!
    WebMock.disable_net_connect!
    $client = HTTPClient.new(base_url: "https://my.cool.service.com")
  end

  it "caches nil responses for missing stubs" do
    expect {
      $client.get('info')
    }.to raise_error(WebMock::NetConnectNotAllowedError)
  end

  it "fails afterwards even if correct stub is present" do
    stub = WebMock.stub_request(:get, "https://my.cool.service.com/info")
    $client.get('info')
    expect(stub).to have_been_requested
  end
end

The second example fails iff performed in order. It runs fine when ran alone.
This example is the result of hours of debugging where I finally found that HTTPClientAdapter#webmock_responses caches responses.
Because we have a test where the client is hold by some kind of adapter which is kind of global (simulated by the $client in the example above), this affects follow.-up tests.

We can work around this problem by monkey-patching HTTPClientAdapter#webmock_responses to

    def webmock_responses
      Hash.new do |hash, request_signature|
        synchronize_request_response do
          hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
        end
      end
    end

but probably, the memoization is there fore a reason, isn’t it?

@bblimke
Copy link
Owner

bblimke commented Aug 20, 2023

@toaster thank you for reporting and for spending time identifying the problem with memoization.

This is the original commit where memoization has been introduced 8786b65

I agree that there should be nothing left in the client instance after the request.

Perhaps that's what is causing the random failures when running WebMock specs #711

I will investigate that when I find some time or perhaps someone else will have time to do that.

@bblimke
Copy link
Owner

bblimke commented Feb 21, 2024

@toaster this is now fixed in version 3.22.0

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

2 participants