diff --git a/CHANGELOG.md b/CHANGELOG.md index e3d72b8f6..26f241783 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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 diff --git a/lib/rubocop/cop/rspec/expect_actual.rb b/lib/rubocop/cop/rspec/expect_actual.rb index ea1fa0920..e3cb6daad 100644 --- a/lib/rubocop/cop/rspec/expect_actual.rb +++ b/lib/rubocop/cop/rspec/expect_actual.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/rspec/expect_actual_spec.rb b/spec/rubocop/cop/rspec/expect_actual_spec.rb index 8fe4ebe62..05d6370cd 100644 --- a/spec/rubocop/cop/rspec/expect_actual_spec.rb +++ b/spec/rubocop/cop/rspec/expect_actual_spec.rb @@ -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) { {} }