-
-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RSpec/UnspecifiedException appears to trigger if the subject function is named raise_exception
#1949
Comments
In our code, it is a public method (obviously not public public, but y'know). I will take a look at this tomorrow (US Central) and see if I can submit a PR myself. :) |
I am having trouble understanding how to implement this fix, sorry. I don't fully understand the parsing abstractions being done, and I have no experience here. :( I will try to stare at it some more, but I feel like I am out of my depth here. |
This is really easy, as in either case you won’t have a o dig i to the abstract syntax tree or node patterns.
You can implement either fix or both. Please let me know if you need more details, and please don’t hesitate to ask if you get stuck. |
Thank you for this explanation! I apologize for not responding sooner - I have had a lot going on. I would be happy to take a look at doing a quick PR. I did get as far as creating a failing test case, at least. :)
|
Hey! I tried implementing both of these like this in def find_expect_to(node)
return if node.receiver.nil?
node.each_ancestor.find do |ancestor|
break if ancestor.block_type?
next unless ancestor.send_type?
expect_to?(ancestor)
end
end and added the following tests to the spec: it 'allows a subject function to be named raise_exception' do
expect_no_offenses(<<~RUBY)
def raise_exception
raise StandardError
end
expect {
raise_exception
}.to raise_error(StandardError)
RUBY
end
it 'allows a subject function to be named raise_error' do
expect_no_offenses(<<~RUBY)
def raise_error
raise StandardError
end
expect {
raise_error
}.to raise_exception(StandardError)
RUBY
end This caused my new test cases to pass, but it caused several regressions in the it 'detects the `unspecified_exception` offense' do
expect_offense(<<~RUBY)
expect {
raise StandardError
}.to raise_error
^^^^^^^^^^^ Specify the exception being captured
RUBY
end with this message: 0) RuboCop::Cop::RSpec::UnspecifiedException with raise_error matcher detects the `unspecified_exception` offense
Failure/Error: super
Diff:
@@ -1,5 +1,4 @@
expect {
raise StandardError
}.to raise_error
- ^^^^^^^^^^^ Specify the exception being captured
# ./spec/support/expect_offense.rb:17:in `expect_offense'
# ./spec/rubocop/cop/rspec/unspecified_exception_spec.rb:6:in `block (3 levels) in <top (required)>' Apologies for the dump - I am still fairly new with Ruby, and in particular, the internals of RuboCop. |
@aarestad I believe that @pirj was meaning you should implement one of those fixes, not both of them, and it seems that option 1 is the better way to go: if you remove the Also two tips I've learnt from my contributions:
You might have already figured these out but since you mentioned this was the first time you'd worked with the Rubocop code, I figure no harm in sharing as those two things were probably the most impactful to figure out when I was getting started 🙂 |
Huh - I could have sworn I tried to do them each on their own. Looks ok - I am going to create a PR now. Thank you all for checking my work in advance :) |
It seems like the
RSpec/UnspecifiedException
cop should not be triggered on the following code, but it is:In particular, the cop highlights
subject.raise_exception
as violating the cop, which doesn't make sense - the cop should be looking it how we handle the exception, yeah?The text was updated successfully, but these errors were encountered: