{"record":{"id":"f955bcfc6a1e1126","repo":"bblimke/webmock","slug":"with-method-invoked-with-no-arguments-either-opt","errorCode":null,"errorMessage":"#with method invoked with no arguments. Either options hash or block must be specified. Created a block with do..end? Try creating it with curly braces {} instead.","messagePattern":"#with method invoked with no arguments\\. Either options hash or block must be specified\\. Created a block with do\\.\\.end\\? Try creating it with curly braces (.+?) instead\\.","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/webmock/request_pattern.rb","lineNumber":29,"sourceCode":"      matcher.class.name =~ /R?Spec::Mocks::ArgumentMatchers::HashExcludingMatcher/\n    end\n  end\n\n  class RequestPattern\n\n    attr_reader :method_pattern, :uri_pattern, :body_pattern, :headers_pattern\n\n    def initialize(method, uri, options = {})\n      @method_pattern  = MethodPattern.new(method)\n      @uri_pattern     = create_uri_pattern(uri)\n      @body_pattern    = nil\n      @headers_pattern = nil\n      @with_block      = nil\n      assign_options(options)\n    end\n\n    def with(options = {}, &block)\n      raise ArgumentError.new('#with method invoked with no arguments. Either options hash or block must be specified. Created a block with do..end? Try creating it with curly braces {} instead.') if options.empty? && !block_given?\n      assign_options(options)\n      @with_block = block\n      self\n    end\n\n    def matches?(request_signature)\n      content_type = request_signature.headers['Content-Type'] if request_signature.headers\n      content_type = content_type.split(';').first if content_type\n      @method_pattern.matches?(request_signature.method) &&\n        @uri_pattern.matches?(request_signature.uri) &&\n        (@body_pattern.nil? || @body_pattern.matches?(request_signature.body, content_type || \"\")) &&\n        (@headers_pattern.nil? || @headers_pattern.matches?(request_signature.headers)) &&\n        (@with_block.nil? || @with_block.call(request_signature))\n    end\n\n    def to_s\n      string = \"#{@method_pattern.to_s.upcase}\".dup\n      string << \" #{@uri_pattern.to_s}\"","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/bblimke/webmock/blob/b187df8827e1f08a1684a8ddc0a5050dbd449c16/lib/webmock/request_pattern.rb#L11-L47","documentation":"RequestPattern#with must receive either a non-empty options hash (body, headers, query, basic_auth) or a block; with an empty hash and no block the call is ambiguous, so webmock raises ArgumentError immediately (lib/webmock/request_pattern.rb:29). The do..end hint is about Ruby block precedence: in a longer chain a do..end block can bind to a different method than intended, leaving with() with no block at all, while curly braces bind to the nearest method. An options hash built at runtime can also collapse to {} when every conditional entry is skipped.","triggerScenarios":"Calling .with({}) or .with() with no arguments. Building options dynamically (opts = {}; opts[:body] = b if condition; stub_request(:get, url).with(opts)) when the condition is false so opts stays empty. Attaching the constraint block with do..end to the wrong method of a chain so with() receives neither options nor a block. Shared examples that forward an optional with: hash that defaults to {}","commonSituations":"Shared stub helpers that accept an optional options hash and forward it blindly; refactoring that moves a body/headers constraint into a conditional branch; copy-pasting README block examples but using do..end inside a bigger method chain; a typo like .with(body:) where the value on the next line is an empty hash.","solutions":["Give with at least one real constraint: .with(body: {...}), .with(headers: {...}) or .with(query: {...})","Or express the constraint as a block with curly braces: .with { |req| req.body.include?('token') }","If no narrowing is needed, remove the .with call entirely - stub_request(:get, url) already matches method and URI","For dynamic options, only chain with when the hash is non-empty: stub = a_request(:get, url); stub = stub.with(opts) unless opts.empty?"],"exampleFix":"# before\nopts = {}\nopts[:query] = { page: 2 } if paginated\nstub_request(:get, 'https://api.example.com/items').with(opts)  # ArgumentError when paginated is false\n\n# after\nopts = {}\nopts[:query] = { page: 2 } if paginated\nstub = stub_request(:get, 'https://api.example.com/items')\nstub = stub.with(opts) unless opts.empty?","handlingStrategy":"validation","validationCode":"# Helper that never calls .with with empty options:\ndef apply_with(stub, opts = {}, &blk)\n  opts.empty? && blk.nil? ? stub : stub.with(opts, &blk)\nend\n\nstub = apply_with(stub_request(:get, url), conditional_opts)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default helper parameters to nil instead of {} and branch on presence","Prefer curly-brace blocks with .when chaining methods","In shared examples, raise a clear error yourself when a forwarded with: hash arrives empty"],"tags":["webmock","argumenterror","request-matching","ruby","block-precedence"],"backgroundTag":"missing-required-argument","analyzedSha":"b187df8827e1f08a1684a8ddc0a5050dbd449c16","analyzedAt":"2026-08-23T02:55:22.028Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}