Skip to content

Commit 0be6148

Browse files
authored
Merge pull request #1152 from tejasbubane/fix-1143
Fix false negative in `RSpec/ExpectChange` cop with block style and chained method call
2 parents 9e55194 + 0cbc140 commit 0be6148

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Fix an exception in `DescribedClass` when accessing a constant on a variable in a spec that is nested in a namespace. ([@rrosenblum][])
77
* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
88
* Add `RSpec/Rails/AvoidSetupHook cop. ([@paydaylight][])
9+
* Fix false negative in `RSpec/ExpectChange` cop with block style and chained method call. ([@tejasbubane][])
910

1011
## 2.3.0 (2021-04-28)
1112

lib/rubocop/cop/rspec/expect_change.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ExpectChange < Base
3939

4040
# @!method expect_change_with_arguments(node)
4141
def_node_matcher :expect_change_with_arguments, <<-PATTERN
42-
(send nil? :change ({const send} nil? $_) (sym $_))
42+
(send nil? :change $_ (sym $_))
4343
PATTERN
4444

4545
# @!method expect_change_with_block(node)
@@ -55,9 +55,9 @@ def on_send(node)
5555
return unless style == :block
5656

5757
expect_change_with_arguments(node) do |receiver, message|
58-
msg = format(MSG_CALL, obj: receiver, attr: message)
58+
msg = format(MSG_CALL, obj: receiver.source, attr: message)
5959
add_offense(node, message: msg) do |corrector|
60-
replacement = "change { #{receiver}.#{message} }"
60+
replacement = "change { #{receiver.source}.#{message} }"
6161
corrector.replace(node, replacement)
6262
end
6363
end

spec/rubocop/cop/rspec/expect_change_spec.rb

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
RUBY
6868
end
6969

70-
it 'flags change matcher when receiver is a variable' do
70+
it 'flags change matcher when receiver is a constant' do
7171
expect_offense(<<-RUBY)
7272
it do
7373
expect { run }.to change(User, :count)
@@ -82,6 +82,81 @@
8282
RUBY
8383
end
8484

85+
it 'flags change matcher when receiver is a top-level constant' do
86+
expect_offense(<<-RUBY)
87+
it do
88+
expect { run }.to change(::User, :count)
89+
^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { ::User.count }`.
90+
end
91+
RUBY
92+
93+
expect_correction(<<-RUBY)
94+
it do
95+
expect { run }.to change { ::User.count }
96+
end
97+
RUBY
98+
end
99+
100+
it 'flags change matcher when receiver is a variable' do
101+
expect_offense(<<-RUBY)
102+
it do
103+
expect { run }.to change(user, :status)
104+
^^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.status }`.
105+
end
106+
RUBY
107+
108+
expect_correction(<<-RUBY)
109+
it do
110+
expect { run }.to change { user.status }
111+
end
112+
RUBY
113+
end
114+
115+
it 'registers an offense for change matcher with chained method call' do
116+
expect_offense(<<-RUBY)
117+
it do
118+
expect { paint_users! }.to change(users.green, :count).by(1)
119+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { users.green.count }`.
120+
end
121+
RUBY
122+
123+
expect_correction(<<-RUBY)
124+
it do
125+
expect { paint_users! }.to change { users.green.count }.by(1)
126+
end
127+
RUBY
128+
end
129+
130+
it 'registers an offense for change matcher with an instance variable' do
131+
expect_offense(<<-RUBY)
132+
it do
133+
expect { paint_users! }.to change(@food, :taste).to(:sour)
134+
^^^^^^^^^^^^^^^^^^^^^ Prefer `change { @food.taste }`.
135+
end
136+
RUBY
137+
138+
expect_correction(<<-RUBY)
139+
it do
140+
expect { paint_users! }.to change { @food.taste }.to(:sour)
141+
end
142+
RUBY
143+
end
144+
145+
it 'registers an offense for change matcher with a global variable' do
146+
expect_offense(<<-RUBY)
147+
it do
148+
expect { paint_users! }.to change($token, :value).to(nil)
149+
^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { $token.value }`.
150+
end
151+
RUBY
152+
153+
expect_correction(<<-RUBY)
154+
it do
155+
expect { paint_users! }.to change { $token.value }.to(nil)
156+
end
157+
RUBY
158+
end
159+
85160
it 'ignores methods called change' do
86161
expect_no_offenses(<<-RUBY)
87162
it do

0 commit comments

Comments
 (0)