bblimke/webmock · warning · RuntimeError

Request stub #{stub.to_s} is not registered.

Error message

Request stub 

 #{stub.to_s} 

 is not registered.

What it means

StubRegistry#remove_request_stub (public as WebMock::API.remove_request_stub) deletes a stub from the registry; when Array#delete finds nothing it raises RuntimeError 'Request stub ... is not registered.' It means the exact stub object is not currently in the registry - double removal, removal after a reset, or a stub that was never registered.

Source

Thrown at lib/webmock/stub_registry.rb:57

          update_response.call
        else
          response_lock.synchronize(&update_response)
        end
      }.to_return(lambda { |request|
        response_lock.synchronize { responses.delete(request.object_id) }
      })

      global_stubs[order].push stub
    end

    def register_request_stub(stub)
      request_stubs.insert(0, stub)
      stub
    end

    def remove_request_stub(stub)
      if not request_stubs.delete(stub)
        raise "Request stub \n\n #{stub.to_s} \n\n is not registered."
      end
    end

    def registered_request?(request_signature)
      request_stub_for(request_signature)
    end

    def response_for_request(request_signature)
      stub = request_stub_for(request_signature)
      stub ? evaluate_response_for_request(stub.response, request_signature) : nil
    end

    private

    def request_stub_for(request_signature)
      (global_stubs[:before_local_stubs] + request_stubs + global_stubs[:after_local_stubs])
        .detect { |registered_request_stub|
          registered_request_stub.request_pattern.matches?(request_signature)

View on GitHub (pinned to b187df8827)

Solutions

  1. Remove each stub exactly once - choose manual removal OR WebMock.reset!, not both
  2. Guard with registry state: registry = WebMock::StubRegistry.instance; registry.remove_request_stub(stub) if registry.request_stubs.include?(stub)
  3. In generic cleanup helpers, rescue RuntimeError and re-raise only when the message does not match /is not registered/
  4. Only remove stubs created via stub_request, which registers them on creation

Example fix

// before
WebMock.remove_request_stub(stub)
WebMock.remove_request_stub(stub) # second call raises

// after
registry = WebMock::StubRegistry.instance
registry.remove_request_stub(stub) if registry.request_stubs.include?(stub)
Defensive patterns

Strategy: validation

Validate before calling

registry = WebMock::StubRegistry.instance
registry.remove_request_stub(stub) if registry.request_stubs.include?(stub)

Try / catch

begin
  WebMock.remove_request_stub(stub)
rescue RuntimeError => e
  raise unless e.message.include?('is not registered')
  # already removed or the registry was reset - nothing left to do
end

Prevention

When it happens

Trigger: Calling WebMock.remove_request_stub(stub) twice for the same stub; removing a stub after WebMock.reset! or test-framework teardown already cleared the registry; removing a WebMock::RequestStub instance built directly with .new (never passed through stub_request); two cleanup hooks (RSpec around + after) both removing the same stub.

Common situations: Custom teardown hooks racing webmock's own reset; shared contexts that remove stubs manually and then reset again; stub objects passed between helpers and removed in more than one place; duplicated cleanup code after refactoring.

Related errors


AI-assisted analysis of bblimke/webmock@b187df8827 (2026-08-23). Data as JSON: /api/errors/55944bd18fc8947f. Report an issue: GitHub.