{"record":{"id":"caa81e97dc7c7a3e","repo":"bblimke/webmock","slug":"wrong-order-use-before-local-stubs-or-after-loc","errorCode":null,"errorMessage":"Wrong order. Use :before_local_stubs or :after_local_stubs","messagePattern":"Wrong order\\. Use :before_local_stubs or :after_local_stubs","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/webmock/stub_registry.rb","lineNumber":24,"sourceCode":"    include Singleton\n\n    attr_accessor :request_stubs\n\n    def initialize\n      reset!\n    end\n\n    def global_stubs\n      @global_stubs ||= Hash.new { |h, k| h[k] = [] }\n    end\n\n    def reset!\n      self.request_stubs = []\n    end\n\n    def register_global_stub(order = :before_local_stubs, &block)\n      unless %i[before_local_stubs after_local_stubs].include?(order)\n        raise ArgumentError.new(\"Wrong order. Use :before_local_stubs or :after_local_stubs\")\n      end\n\n      # This hash contains the responses returned by the block,\n      # keyed by the exact request (using the object_id).\n      # That way, there's no race condition in case #to_return\n      # doesn't run immediately after stub.with.\n      responses = {}\n      response_lock = Mutex.new\n\n      stub = ::WebMock::RequestStub.new(:any, ->(uri) { true }).with { |request|\n        update_response = -> { responses[request.object_id] = yield(request) }\n\n        # The block can recurse, so only lock if we don't already own it\n        if response_lock.owned?\n          update_response.call\n        else\n          response_lock.synchronize(&update_response)\n        end","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/bblimke/webmock/blob/b187df8827e1f08a1684a8ddc0a5050dbd449c16/lib/webmock/stub_registry.rb#L6-L42","documentation":"WebMock.globally_stub_request(order = :before_local_stubs, &block) registers a global stub consulted on every request; StubRegistry#register_global_stub accepts only the symbols :before_local_stubs and :after_local_stubs (lib/webmock/stub_registry.rb:24), which decide whether the global stub wins over, or yields to, test-local stub_request stubs. Any other value - :before, :after, a string like 'before_local_stubs', or a typo - raises ArgumentError, because %i[before_local_stubs after_local_stubs].include?(order) compares symbols strictly and never matches strings.","triggerScenarios":"WebMock.globally_stub_request(:before) { |req| ... } copied from an old blog post or outdated team docs. Passing 'after_local_stubs' as a String - the symbol-array include? fails on strings. An order value read from YAML/ENV where symbols became strings or were misspelled.","commonSituations":"Global stubs for third-party APIs set up in rails_helper/spec_helper; documentation written against an older webmock API; config-driven spec setup that passes through untyped strings.","solutions":["Use the exact symbols: WebMock.globally_stub_request(:before_local_stubs) { |request_signature| ... } or :after_local_stubs","If precedence does not matter, omit the argument - :before_local_stubs is the default","If the order value comes from config, normalize it first: order.to_s.to_sym validated against the two allowed symbols"],"exampleFix":"# before\nWebMock.globally_stub_request('before') { |req| ... }  # ArgumentError: Wrong order\n\n# after\nWebMock.globally_stub_request(:before_local_stubs) { |req| ... }","handlingStrategy":"validation","validationCode":"STUB_ORDER = %i[before_local_stubs after_local_stubs].freeze\norder = STUB_ORDER.include?(order) ? order : :before_local_stubs\nWebMock.globally_stub_request(order, &block)","typeGuard":"def valid_stub_order?(o)\n  %i[before_local_stubs after_local_stubs].include?(o)\nend","tryCatchPattern":"begin\n  WebMock.globally_stub_request(order, &block)\nrescue ArgumentError\n  WebMock.globally_stub_request(:before_local_stubs, &block)\nend","preventionTips":["Define the order as a spec-helper constant","Never source the order from untyped config strings without to_sym","Remember a String never matches the symbol allowlist"],"tags":["webmock","global-stubs","argumenterror","ruby","api-misuse"],"backgroundTag":"invalid-enum-value","analyzedSha":"b187df8827e1f08a1684a8ddc0a5050dbd449c16","analyzedAt":"2026-08-23T02:55:22.028Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}