{"record":{"id":"aaba33116a66916e","repo":"bblimke/webmock","slug":"to-return-json-does-not-support-passing-a-block","errorCode":null,"errorMessage":"#to_return_json does not support passing a block","messagePattern":"#to_return_json does not support passing a block","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/webmock/request_stub.rb","lineNumber":30,"sourceCode":"    end\n\n    def with(params = {}, &block)\n      @request_pattern.with(params, &block)\n      self\n    end\n\n    def to_return(*response_hashes, &block)\n      if block\n        @responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(block)])\n      else\n        @responses_sequences << ResponsesSequence.new([*response_hashes].flatten.map {|r| ResponseFactory.response_for(r)})\n      end\n      self\n    end\n    alias_method :and_return, :to_return\n\n    def to_return_json(*response_hashes)\n      raise ArgumentError, '#to_return_json does not support passing a block' if block_given?\n\n      json_response_hashes = [*response_hashes].flatten.map do |resp_h|\n        headers, body = resp_h.values_at(:headers, :body)\n\n        json_body = if body.respond_to?(:call)\n          ->(request_signature) {\n            b = if body.respond_to?(:arity) && body.arity == 1\n              body.call(request_signature)\n            else\n              body.call\n            end\n            b = b.to_json unless b.is_a?(String)\n            b\n          }\n        elsif !body.is_a?(String)\n          body.to_json\n        else\n          body","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/bblimke/webmock/blob/b187df8827e1f08a1684a8ddc0a5050dbd449c16/lib/webmock/request_stub.rb#L12-L48","documentation":"RequestStub#to_return_json builds JSON responses from one or more response hashes: it flattens the arguments, serializes bodies, and sets the JSON content type. Unlike to_return, it accepts no block - to_return_json(*response_hashes) raises ArgumentError immediately when a block is attached (lib/webmock/request_stub.rb:30). Dynamic JSON responses are still supported: a lambda passed as the :body value is called with the request signature and its return value is serialized to JSON.","triggerScenarios":".to_return_json(status: 200) { ... } - a block, typically left over from copy-pasting a to_return { |request| ... } dynamic stub. Using the and_return_json alias with a block. A spec helper that captures and blindly forwards &block to to_return_json.","commonSituations":"Migrating string-body dynamic stubs to to_return_json and keeping the block; helper methods that always forward a block parameter; examples written against a different stubbing API.","solutions":["Remove the block and put the payload in the hash: .to_return_json(status: 200, body: { id: 1 })","For dynamic JSON, pass a lambda as the :body value: .to_return_json(status: 200, body: ->(request) { { id: request.body[/\\d+/].to_i } })","If status and headers must also vary per request, use the classic form: .to_return(lambda { |request_signature| { status: 200, body: { id: 1 }.to_json } })"],"exampleFix":"# before\nstub.to_return_json(status: 200) { |request| { id: request.body[/\\d+/].to_i } }  # ArgumentError\n\n# after\nstub.to_return_json(status: 200, body: ->(request) { { id: request.body[/\\d+/].to_i } })","handlingStrategy":"validation","validationCode":"# Helper that routes dynamic JSON through :body instead of a block:\ndef stub_json(stub, dynamic: nil, **opts)\n  dynamic ? stub.to_return_json(body: dynamic, **opts) : stub.to_return_json(**opts)\nend","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not forward &block to to_return_json in helper methods","Use a body: lambda for dynamic JSON responses","Enable Lint/UnusedBlockArgument so forgotten blocks surface"],"tags":["webmock","json","stub-response","ruby","api-misuse"],"backgroundTag":"unsupported-argument-combination","analyzedSha":"b187df8827e1f08a1684a8ddc0a5050dbd449c16","analyzedAt":"2026-08-23T02:55:22.028Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}