Skip to content

Commit

Permalink
fix false positive on UnspecifiedException cop when function is named…
Browse files Browse the repository at this point in the history
… raise_exception
  • Loading branch information
aarestad committed Sep 26, 2024
1 parent 16cf19c commit a59315f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix false-positive for `RSpec/UnspecifiedException` when a method is literally named `raise_exception`. ([@aarestad])

## 3.0.5 (2024-09-07)

- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
Expand Down Expand Up @@ -899,6 +901,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.

<!-- Contributors (alphabetically) -->

[@aarestad]: https://github.com/aarestad
[@abrom]: https://github.com/abrom
[@ahukkanen]: https://github.com/ahukkanen
[@akiomik]: https://github.com/akiomik
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/rspec/unspecified_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def empty_exception_matcher?(node)
end

def find_expect_to(node)
node.each_ancestor(:send).find do |ancestor|
node.each_ancestor.find do |ancestor|
break if ancestor.block_type?
next unless ancestor.send_type?

expect_to?(ancestor)
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/rubocop/cop/rspec/unspecified_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@
}.to raise_error(my_exception)
RUBY
end

it 'allows a subject function to be named raise_error' do
expect_no_offenses(<<~RUBY)
def raise_exception
raise StandardError
end
expect {
raise_error
}.to raise_error(StandardError)
RUBY
end

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
end

context 'with raise_exception matcher' do
Expand Down Expand Up @@ -198,5 +222,29 @@
^^^^^^^^^^^^^^^ Specify the exception being captured
RUBY
end

it 'allows a subject function to be named raise_exception' do
expect_no_offenses(<<~RUBY)
def raise_error
raise StandardError
end
expect {
raise_exception
}.to raise_exception(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
end
end

0 comments on commit a59315f

Please # to comment.