Skip to content

Commit 856ee80

Browse files
authored
Merge pull request #1277 from rubocop/fix-expect_change-regression
Fix regression in ExpectChange
2 parents cf27228 + d5104d3 commit 856ee80

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
* Fix a regression in `RSpec/ExpectChange` flagging chained method calls. ([@pirj][])
6+
57
## 2.11.0 (2022-05-18)
68

79
* Drop Ruby 2.5 support. ([@ydah][])

lib/rubocop/cop/rspec/expect_change.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ class ExpectChange < Base
4747
(block
4848
(send nil? :change)
4949
(args)
50-
(send $_ $_)
50+
(send
51+
${
52+
(send nil? _) # change { user.name }
53+
const # change { User.count }
54+
}
55+
$_
56+
)
5157
)
5258
PATTERN
5359

spec/rubocop/cop/rspec/expect_change_spec.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,33 @@
8686
RUBY
8787
end
8888

89-
it 'flags chained method calls' do
89+
it 'flags a method call on an object' do
9090
expect_offense(<<-RUBY)
9191
it do
92-
expect { file.upload! }.to change { user.uploads.count }.by(1)
93-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change(user.uploads, :count)`.
92+
expect { run }.to change { user.name }.to('Jack')
93+
^^^^^^^^^^^^^^^^^^^^ Prefer `change(user, :name)`.
94+
end
95+
RUBY
96+
97+
expect_correction(<<-RUBY)
98+
it do
99+
expect { run }.to change(user, :name).to('Jack')
100+
end
101+
RUBY
102+
end
103+
104+
it 'ignores multiple chained method calls' do
105+
expect_no_offenses(<<-RUBY)
106+
it do
107+
expect { run }.to change { user.reload.name }
108+
end
109+
RUBY
110+
end
111+
112+
it 'ignores a variable/method' do
113+
expect_no_offenses(<<-RUBY)
114+
it do
115+
expect { run }.to change { results }.to([])
94116
end
95117
RUBY
96118
end

0 commit comments

Comments
 (0)