Skip to content

Commit 0cbc140

Browse files
committed
Add more specs for RSpec/ExpectChange
1 parent c3dd64a commit 0cbc140

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

spec/rubocop/cop/rspec/expect_change_spec.rb

Lines changed: 61 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,36 @@
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+
85115
it 'registers an offense for change matcher with chained method call' do
86116
expect_offense(<<-RUBY)
87117
it do
@@ -97,6 +127,36 @@
97127
RUBY
98128
end
99129

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+
100160
it 'ignores methods called change' do
101161
expect_no_offenses(<<-RUBY)
102162
it do

0 commit comments

Comments
 (0)