diff --git a/CHANGELOG.md b/CHANGELOG.md index eb49b855e..205dbb8fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +* Fix a regression in `RSpec/ExpectChange` flagging chained method calls. ([@pirj][]) + ## 2.11.0 (2022-05-18) * Drop Ruby 2.5 support. ([@ydah][]) diff --git a/lib/rubocop/cop/rspec/expect_change.rb b/lib/rubocop/cop/rspec/expect_change.rb index 779e9ec0e..51c19bd28 100644 --- a/lib/rubocop/cop/rspec/expect_change.rb +++ b/lib/rubocop/cop/rspec/expect_change.rb @@ -47,7 +47,13 @@ class ExpectChange < Base (block (send nil? :change) (args) - (send $_ $_) + (send + ${ + (send nil? _) # change { user.name } + const # change { User.count } + } + $_ + ) ) PATTERN diff --git a/spec/rubocop/cop/rspec/expect_change_spec.rb b/spec/rubocop/cop/rspec/expect_change_spec.rb index 6ef9378b7..3ec1fca31 100644 --- a/spec/rubocop/cop/rspec/expect_change_spec.rb +++ b/spec/rubocop/cop/rspec/expect_change_spec.rb @@ -86,11 +86,33 @@ RUBY end - it 'flags chained method calls' do + it 'flags a method call on an object' do expect_offense(<<-RUBY) it do - expect { file.upload! }.to change { user.uploads.count }.by(1) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change(user.uploads, :count)`. + expect { run }.to change { user.name }.to('Jack') + ^^^^^^^^^^^^^^^^^^^^ Prefer `change(user, :name)`. + end + RUBY + + expect_correction(<<-RUBY) + it do + expect { run }.to change(user, :name).to('Jack') + end + RUBY + end + + it 'ignores multiple chained method calls' do + expect_no_offenses(<<-RUBY) + it do + expect { run }.to change { user.reload.name } + end + RUBY + end + + it 'ignores a variable/method' do + expect_no_offenses(<<-RUBY) + it do + expect { run }.to change { results }.to([]) end RUBY end