Skip to content

Commit

Permalink
Merge pull request #1152 from tejasbubane/fix-1143
Browse files Browse the repository at this point in the history
Fix false negative in `RSpec/ExpectChange` cop with block style and chained method call
  • Loading branch information
pirj authored May 25, 2021
2 parents 9e55194 + 0cbc140 commit 0be6148
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fix an exception in `DescribedClass` when accessing a constant on a variable in a spec that is nested in a namespace. ([@rrosenblum][])
* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])
* Add `RSpec/Rails/AvoidSetupHook cop. ([@paydaylight][])
* Fix false negative in `RSpec/ExpectChange` cop with block style and chained method call. ([@tejasbubane][])

## 2.3.0 (2021-04-28)

Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rspec/expect_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ExpectChange < Base

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

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

expect_change_with_arguments(node) do |receiver, message|
msg = format(MSG_CALL, obj: receiver, attr: message)
msg = format(MSG_CALL, obj: receiver.source, attr: message)
add_offense(node, message: msg) do |corrector|
replacement = "change { #{receiver}.#{message} }"
replacement = "change { #{receiver.source}.#{message} }"
corrector.replace(node, replacement)
end
end
Expand Down
77 changes: 76 additions & 1 deletion spec/rubocop/cop/rspec/expect_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
RUBY
end

it 'flags change matcher when receiver is a variable' do
it 'flags change matcher when receiver is a constant' do
expect_offense(<<-RUBY)
it do
expect { run }.to change(User, :count)
Expand All @@ -82,6 +82,81 @@
RUBY
end

it 'flags change matcher when receiver is a top-level constant' do
expect_offense(<<-RUBY)
it do
expect { run }.to change(::User, :count)
^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { ::User.count }`.
end
RUBY

expect_correction(<<-RUBY)
it do
expect { run }.to change { ::User.count }
end
RUBY
end

it 'flags change matcher when receiver is a variable' do
expect_offense(<<-RUBY)
it do
expect { run }.to change(user, :status)
^^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.status }`.
end
RUBY

expect_correction(<<-RUBY)
it do
expect { run }.to change { user.status }
end
RUBY
end

it 'registers an offense for change matcher with chained method call' do
expect_offense(<<-RUBY)
it do
expect { paint_users! }.to change(users.green, :count).by(1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { users.green.count }`.
end
RUBY

expect_correction(<<-RUBY)
it do
expect { paint_users! }.to change { users.green.count }.by(1)
end
RUBY
end

it 'registers an offense for change matcher with an instance variable' do
expect_offense(<<-RUBY)
it do
expect { paint_users! }.to change(@food, :taste).to(:sour)
^^^^^^^^^^^^^^^^^^^^^ Prefer `change { @food.taste }`.
end
RUBY

expect_correction(<<-RUBY)
it do
expect { paint_users! }.to change { @food.taste }.to(:sour)
end
RUBY
end

it 'registers an offense for change matcher with a global variable' do
expect_offense(<<-RUBY)
it do
expect { paint_users! }.to change($token, :value).to(nil)
^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { $token.value }`.
end
RUBY

expect_correction(<<-RUBY)
it do
expect { paint_users! }.to change { $token.value }.to(nil)
end
RUBY
end

it 'ignores methods called change' do
expect_no_offenses(<<-RUBY)
it do
Expand Down

0 comments on commit 0be6148

Please sign in to comment.