From f31f01d49cb15622c4bd970e1021c40e8276af49 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Tue, 27 Jan 2026 21:59:20 +0100 Subject: [PATCH] Fix before_each method to handle nil parameter correctly - Fix NoMethodError when calling before_each(nil) by clearing the array instead of assigning nil - Update test expectations to be more specific about error types and messages --- CHANGELOG.md | 2 ++ lib/grape/endpoint.rb | 4 +++- spec/grape/endpoint_spec.rb | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afb9819d2..071998afe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Fixes * Your contribution here. +* [#2655](https://github.com/ruby-grape/grape/pull/2655): Fix `before_each` method to handle `nil` parameter correctly - [@ericproulx](https://github.com/ericproulx). ### 3.1.0 (2026-01-25) @@ -33,6 +34,7 @@ * [#2633](https://github.com/ruby-grape/grape/pull/2633): Fix cascade reading - [@ericproulx](https://github.com/ericproulx). * [#2641](https://github.com/ruby-grape/grape/pull/2641): Restore support for `return` in endpoint blocks - [@ericproulx](https://github.com/ericproulx). * [#2642](https://github.com/ruby-grape/grape/pull/2642): Fix array allocation in base_route.rb - [@ericproulx](https://github.com/ericproulx). +* Fix `before_each` method to handle `nil` parameter correctly - [@ericproulx](https://github.com/ericproulx). ### 3.0.1 (2025-11-24) diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 041b89640..c8361faac 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -23,8 +23,10 @@ def before_each(new_setup = false, &block) return @before_each unless block @before_each << block - else + elsif new_setup @before_each = [new_setup] + else + @before_each.clear end end diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index b82672aa1..27c9f2736 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -24,7 +24,7 @@ def app it 'is able to override a helper' do subject.get('/') { current_user } - expect { get '/' }.to raise_error(NameError) + expect { get '/' }.to raise_error(NameError, /undefined local variable or method [`']current_user'/) described_class.before_each do |endpoint| allow(endpoint).to receive(:current_user).and_return('Bob') @@ -34,7 +34,7 @@ def app expect(last_response.body).to eq('Bob') described_class.before_each(nil) - expect { get '/' }.to raise_error(NameError) + expect { get '/' }.to raise_error(NameError, /undefined local variable or method [`']current_user'/) end it 'is able to stack helper' do @@ -42,7 +42,7 @@ def app authenticate_user! current_user end - expect { get '/' }.to raise_error(NameError) + expect { get '/' }.to raise_error(NoMethodError, /undefined method [`']authenticate_user!' for/) described_class.before_each do |endpoint| allow(endpoint).to receive(:current_user).and_return('Bob') @@ -56,7 +56,7 @@ def app expect(last_response.body).to eq('Bob') described_class.before_each(nil) - expect { get '/' }.to raise_error(NameError) + expect { get '/' }.to raise_error(NoMethodError, /undefined method [`']authenticate_user!' for/) end end