Fix NoMethodError when before_each is reset with nil#2654
Fix NoMethodError when before_each is reset with nil#2654heromoon9218 wants to merge 3 commits intoruby-grape:masterfrom
NoMethodError when before_each is reset with nil#2654Conversation
264c201 to
6ff2831
Compare
NoMethodError when before_each is reset with nil
Danger ReportNo issues found. |
|
Hi @heromoon9218, thanks for opening the PR 💪 Could you add a spec to cover this case ? I'm still wondering how it's possible other than calling a callback like |
|
Thank you for your feedback 🙏 I've added a spec to cover this case. My use case: # Setup
Grape::Endpoint.before_each do |endpoint|
allow(endpoint).to receive(:current_user).and_return(user)
end
# Teardown
Grape::Endpoint.before_each nilWhy existing specs didn't catch this: described_class.before_each(nil)
expect { get '/' }.to raise_error(NameError)Since |
| allow(endpoint).to receive(:current_user).and_return('Bob') | ||
| end | ||
|
|
||
| described_class.before_each nil |
There was a problem hiding this comment.
I think it would make more sense to put it after the expect. What do you think ?
There was a problem hiding this comment.
I think it would make more sense to put it after the expect. What do you think ?
Just to clarify - are you referring to the new spec at L69, or the existing specs at L36-37 and L58-59 ?
If you mean L69, moving before_each nil after the expect would change the test's intent. In my understanding, the test should verify that a request after before_each nil does not raise NoMethodError.
Apologies if I misunderstood your suggestion 🙏
There was a problem hiding this comment.
I think I understand the issue with the specs. raise_error(NameError) also captures NoMethodError since the latter inherits from NameError. There are already some tests that should have captured that error. I'll open a PR to fix it. Are you ok with that ?
There was a problem hiding this comment.
There are already some tests that should have captured that error. I'll open a PR to fix it. Are you ok with that ?
Yes, absolutely ! Thank you for your support 🙌
|
@heromoon9218 Could you test #2655 ? |
I tested #2655 by updating my project's Gemfile to: gem "grape", github: "ruby-grape/grape", ref: "f31f01d49cb15622c4bd970e1021c40e8276af49"All tests passed on CI. The fix works as expected. Thank you for your help! Should I close this PR ? |
|
Closing in favor of #2655. |
Summary
Fix
NoMethodError: undefined method 'call' for nilwhenGrape::Endpoint.before_each nilis called to reset the before_each callbacks.Problem
After #2643 removed
trymethod usage from the codebase,run_before_eachwas changed from:to:
This causes a
NoMethodErrorwhenbefore_each nilis called.