diff --git a/CHANGELOG.md b/CHANGELOG.md index c23b05dc1..cb21d1aa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Bump RuboCop requirement to +1.81. ([@ydah]) - Fix a false positive for `RSpec/LetSetup` when `let!` used in outer scope. ([@ydah]) - Fix a false positive for `RSpec/ReceiveNever` cop when `allow(...).to receive(...).never`. ([@ydah]) +- Fix detection of nameless doubles with methods in `RSpec/VerifiedDoubles`. ([@ushi-as]) ## 3.7.0 (2025-09-01) @@ -1082,6 +1083,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@tmaier]: https://github.com/tmaier [@topalovic]: https://github.com/topalovic [@twalpole]: https://github.com/twalpole +[@ushi-as]: https://github.com/ushi-as [@vzvu3k6k]: https://github.com/vzvu3k6k [@walf443]: https://github.com/walf443 [@yasu551]: https://github.com/yasu551 diff --git a/lib/rubocop/cop/rspec/verified_doubles.rb b/lib/rubocop/cop/rspec/verified_doubles.rb index 7f2c3cfb1..ee42c324a 100644 --- a/lib/rubocop/cop/rspec/verified_doubles.rb +++ b/lib/rubocop/cop/rspec/verified_doubles.rb @@ -78,7 +78,7 @@ class VerifiedDoubles < Base def on_send(node) unverified_double(node) do |name, *_args| - return if name.nil? && cop_config['IgnoreNameless'] + return if (name.nil? || hash?(name)) && cop_config['IgnoreNameless'] return if symbol?(name) && cop_config['IgnoreSymbolicNames'] add_offense(node) @@ -90,6 +90,10 @@ def on_send(node) def symbol?(name) name&.sym_type? end + + def hash?(arg) + arg.hash_type? + end end end end diff --git a/spec/rubocop/cop/rspec/verified_doubles_spec.rb b/spec/rubocop/cop/rspec/verified_doubles_spec.rb index a64407201..24870fd91 100644 --- a/spec/rubocop/cop/rspec/verified_doubles_spec.rb +++ b/spec/rubocop/cop/rspec/verified_doubles_spec.rb @@ -64,6 +64,15 @@ end RUBY end + + it 'flags doubles that have no name but methods specified' do + expect_offense(<<~RUBY) + it do + foo = double(call: :bar) + ^^^^^^^^^^^^^^^^^^ Prefer using verifying doubles over normal doubles. + end + RUBY + end end it 'ignores doubles that have no name specified' do @@ -74,6 +83,14 @@ RUBY end + it 'ignores doubles that have no name but methods specified' do + expect_no_offenses(<<~RUBY) + it do + foo = double(call: :bar) + end + RUBY + end + it 'ignores instance_doubles' do expect_no_offenses(<<~RUBY) it do