Skip to content

Commit e340569

Browse files
committed
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
1 parent 2bd3145 commit e340569

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#2655](https://github.com/ruby-grape/grape/pull/2655): Fix `before_each` method to handle `nil` parameter correctly - [@ericproulx](https://github.com/ericproulx).
1011

1112
### 3.1.0 (2026-01-25)
1213

@@ -33,6 +34,7 @@
3334
* [#2633](https://github.com/ruby-grape/grape/pull/2633): Fix cascade reading - [@ericproulx](https://github.com/ericproulx).
3435
* [#2641](https://github.com/ruby-grape/grape/pull/2641): Restore support for `return` in endpoint blocks - [@ericproulx](https://github.com/ericproulx).
3536
* [#2642](https://github.com/ruby-grape/grape/pull/2642): Fix array allocation in base_route.rb - [@ericproulx](https://github.com/ericproulx).
37+
* Fix `before_each` method to handle `nil` parameter correctly - [@ericproulx](https://github.com/ericproulx).
3638

3739
### 3.0.1 (2025-11-24)
3840

lib/grape/endpoint.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def before_each(new_setup = false, &block)
2323
return @before_each unless block
2424

2525
@before_each << block
26-
else
26+
elsif new_setup
2727
@before_each = [new_setup]
28+
else
29+
@before_each.clear
2830
end
2931
end
3032

spec/grape/endpoint_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def app
2424

2525
it 'is able to override a helper' do
2626
subject.get('/') { current_user }
27-
expect { get '/' }.to raise_error(NameError)
27+
expect { get '/' }.to raise_error(NameError, /undefined local variable or method 'current_user'/)
2828

2929
described_class.before_each do |endpoint|
3030
allow(endpoint).to receive(:current_user).and_return('Bob')
@@ -34,15 +34,15 @@ def app
3434
expect(last_response.body).to eq('Bob')
3535

3636
described_class.before_each(nil)
37-
expect { get '/' }.to raise_error(NameError)
37+
expect { get '/' }.to raise_error(NameError, /undefined local variable or method 'current_user'/)
3838
end
3939

4040
it 'is able to stack helper' do
4141
subject.get('/') do
4242
authenticate_user!
4343
current_user
4444
end
45-
expect { get '/' }.to raise_error(NameError)
45+
expect { get '/' }.to raise_error(NoMethodError, /undefined method 'authenticate_user!' for/)
4646

4747
described_class.before_each do |endpoint|
4848
allow(endpoint).to receive(:current_user).and_return('Bob')
@@ -56,7 +56,7 @@ def app
5656
expect(last_response.body).to eq('Bob')
5757

5858
described_class.before_each(nil)
59-
expect { get '/' }.to raise_error(NameError)
59+
expect { get '/' }.to raise_error(NoMethodError, /undefined method 'authenticate_user!' for/)
6060
end
6161
end
6262

0 commit comments

Comments
 (0)