Skip to content

Commit

Permalink
Add more specs for RSpec/ExpectChange
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed May 25, 2021
1 parent c3dd64a commit 0cbc140
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 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,36 @@
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
Expand All @@ -97,6 +127,36 @@
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 0cbc140

Please sign in to comment.