diff --git a/CHANGELOG.md b/CHANGELOG.md index b2d6365..ba14c68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix a false positive for `RspecRails/NegationBeValid` when use `to_not`. ([@ydah]) + ## 2.32.0 (2025-11-12) - Add `RSpecRails/HttpStatusNameConsistency` cop. ([@taketo1113]) diff --git a/lib/rubocop/cop/rspec_rails/negation_be_valid.rb b/lib/rubocop/cop/rspec_rails/negation_be_valid.rb index c57b228..3f3f66f 100644 --- a/lib/rubocop/cop/rspec_rails/negation_be_valid.rb +++ b/lib/rubocop/cop/rspec_rails/negation_be_valid.rb @@ -38,7 +38,7 @@ class NegationBeValid < ::RuboCop::Cop::Base # @!method not_to?(node) def_node_matcher :not_to?, <<~PATTERN - (send ... :not_to (send nil? :be_valid ...)) + (send ... {:not_to :to_not} (send nil? :be_valid ...)) PATTERN # @!method be_invalid?(node) diff --git a/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb b/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb index d35bf4a..a920e48 100644 --- a/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb +++ b/spec/rubocop/cop/rspec_rails/negation_be_valid_spec.rb @@ -48,6 +48,14 @@ RUBY end + it 'registers an offense when using ' \ + '`expect(...).to_not be_valid`' do + expect_offense(<<~RUBY) + expect(foo).to_not be_valid + ^^^^^^^^^^^^^^^ Use `expect(...).to be_invalid`. + RUBY + end + it 'does not register an offense when using ' \ '`expect(...).to be_invalid`' do expect_no_offenses(<<~RUBY)