{"record":{"id":"a4c43177b72eaa5f","repo":"ruby-grape/grape","slug":"a-block-is-required","errorCode":null,"errorMessage":"a block is required","messagePattern":"a block is required","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/grape/testing.rb","lineNumber":14,"sourceCode":"# frozen_string_literal: true\n\nmodule Grape\n  module Testing\n    module RunBeforeEach\n      def run\n        self.class.run_before_each(self)\n        super\n      end\n    end\n\n    module ClassMethods\n      def before_each(&block)\n        raise ArgumentError, 'a block is required' unless block\n\n        @before_each ||= []\n        @before_each << block\n      end\n\n      def reset_before_each\n        @before_each&.clear\n      end\n\n      def run_before_each(endpoint)\n        superclass.run_before_each(endpoint) unless self == Grape::Endpoint\n        @before_each&.each { |blk| blk.call(endpoint) }\n      end\n    end\n\n    Grape::Endpoint.prepend(RunBeforeEach)\n    Grape::Endpoint.extend(ClassMethods)\n  end","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/ruby-grape/grape/blob/22d7975629846a3c0c7bd2b34e140a7a1b4af8f6/lib/grape/testing.rb#L1-L32","documentation":"Grape::Testing::RunBeforeEach lets tests register endpoint setup hooks with `before_each`, which must be given a block to store. Calling `before_each` without a block (e.g. intending to query or reset hooks) raises ArgumentError ('a block is required') instead of silently registering nothing.","triggerScenarios":"`before_each` bare inside an RSpec setup. `before_each(&:stub_something)`-style shorthand that yields nil instead of a block. Guard code that calls before_each conditionally with the block accidentally dropped.","commonSituations":"Conditionally adding test hooks where the block variable is nil. Refactoring specs from blocks to method references and losing the `&`. Copying spec boilerplate incompletely.","solutions":["Always pass a block: `before_each { |endpoint| endpoint.stub :helper, :value }`.","To clear hooks, call `reset_before_each` instead of a bare before_each.","When the block is optional in your own helpers, guard with `before_each(&block) if block`."],"exampleFix":"# before\nbefore_each # raises\n\n# after\nbefore_each { |endpoint| endpoint.instance_variable_set(:@current_user, user) }\nreset_before_each # when you need to clear instead","handlingStrategy":"validation","validationCode":"before_each(&block) if block # conditional registration\nreset_before_each # clearing hooks instead of a bare before_each","typeGuard":"def block_given?(...) = block_given? # use Ruby's block_given? before calling before_each","tryCatchPattern":null,"preventionTips":["Always pass `&blk` through helper wrappers: `def with_user(user, &blk) before_each(&blk) end`.","Use `reset_before_each` in after-hooks to clear state between specs.","Lint specs for bare `before_each` lines."],"tags":["grape","testing","before-each","block","rspec"],"backgroundTag":"missing-block-argument","analyzedSha":"22d7975629846a3c0c7bd2b34e140a7a1b4af8f6","analyzedAt":"2026-08-21T17:03:54.627Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}