Skip to content

Commit 42f48b9

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 42f48b9

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

PR_DESCRIPTION.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Fix before_each method to handle nil parameter correctly
2+
3+
## Problem
4+
5+
When calling `before_each(nil)` to clear the before_each callbacks, a `NoMethodError` was raised because the code attempted to assign `nil` to `@before_each` instead of clearing the array.
6+
7+
## Solution
8+
9+
- Modified the `before_each` method in `lib/grape/endpoint.rb` to properly handle the `nil` parameter by clearing the `@before_each` array instead of assigning `nil`
10+
- Updated test expectations in `spec/grape/endpoint_spec.rb` to be more specific about error types (`NameError` vs `NoMethodError`) and include error message patterns for better test reliability
11+
12+
## Changes
13+
14+
- `lib/grape/endpoint.rb`: Added `else` clause to clear `@before_each` when `new_setup` is `nil`
15+
- `spec/grape/endpoint_spec.rb`: Updated error expectations to match specific error types and messages
16+
17+
## Testing
18+
19+
All existing tests pass, and the fix ensures that `before_each(nil)` correctly clears the callbacks without raising an error.

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)