Skip to content

Commit

Permalink
Merge pull request #1764 from naveg/naveg/expect-actual-rails-routing
Browse files Browse the repository at this point in the history
Fix a false positive for RSpec/ExpectActual with rspec-rails
  • Loading branch information
pirj authored Jan 9, 2024
2 parents 326f2cf + 30eef5c commit 5a23de1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Support correcting `assert_nil` and `refute_nil` to `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Fix a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])

## 2.26.1 (2024-01-05)

Expand Down Expand Up @@ -894,6 +895,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 5a23de1

Please sign in to comment.