Skip to content

Commit

Permalink
Merge pull request #1277 from rubocop/fix-expect_change-regression
Browse files Browse the repository at this point in the history
Fix regression in ExpectChange
  • Loading branch information
pirj authored May 18, 2022
2 parents cf27228 + d5104d3 commit 856ee80
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
8 changes: 7 additions & 1 deletion lib/rubocop/cop/rspec/expect_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ class ExpectChange < Base
(block
(send nil? :change)
(args)
(send $_ $_)
(send
${
(send nil? _) # change { user.name }
const # change { User.count }
}
$_
)
)
PATTERN

Expand Down
28 changes: 25 additions & 3 deletions spec/rubocop/cop/rspec/expect_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 856ee80

Please sign in to comment.