Skip to content

Commit

Permalink
Fix a false positive for RSpec/ExpectActual with rspec-rails
Browse files Browse the repository at this point in the history
rspec-rails comes with routing matchers that use literals in the
expect()

Fixes #759
  • Loading branch information
naveg committed Jan 5, 2024
1 parent b7d192a commit c53c48e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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 a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])

## 2.26.0 (2024-01-04)

- Add new `RSpec/RedundantPredicateMatcher` cop. ([@ydah])
Expand Down Expand Up @@ -888,6 +890,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@mockdeep]: https://github.com/mockdeep
[@mothonmars]: https://github.com/MothOnMars
[@mvz]: https://github.com/mvz
[@naveg]: https://github.com/naveg
[@nc-holodakg]: https://github.com/nc-holodakg
[@nevir]: https://github.com/nevir
[@ngouy]: https://github.com/ngouy
Expand Down
7 changes: 5 additions & 2 deletions lib/rubocop/cop/rspec/expect_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class ExpectActual < Base
regexp
].freeze

SUPPORTED_MATCHERS = %i[eq eql equal be].freeze
SKIPPED_MATCHERS = %i[route_to be_routable].freeze
CORRECTABLE_MATCHERS = %i[eq eql equal be].freeze

# @!method expect_literal(node)
def_node_matcher :expect_literal, <<~PATTERN
Expand All @@ -66,8 +67,10 @@ class ExpectActual < Base

def on_send(node)
expect_literal(node) do |actual, matcher, expected|
next if SKIPPED_MATCHERS.include?(matcher)

add_offense(actual.source_range) do |corrector|
next unless SUPPORTED_MATCHERS.include?(matcher)
next unless CORRECTABLE_MATCHERS.include?(matcher)
next if literal?(expected)

swap(corrector, actual, expected)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rspec/expect_actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@
expect_no_corrections
end

it 'does not flag when the matcher is a rails routing matcher' do
expect_no_offenses(<<~RUBY)
describe Foo do
it 'routes correctly' do
expect({:get => "foo"}).to be_routable
expect({:get => "foo"}).to route_to("bar#baz")
end
end
RUBY
end

context 'when inspecting rspec-rails routing specs' do
let(:cop_config) { {} }

Expand Down

0 comments on commit c53c48e

Please sign in to comment.