Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new RSpec/ChangeWithoutExpect cop #2074

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ydah
Copy link
Member

@ydah ydah commented Apr 4, 2025

Follow up: #2071


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@ydah ydah requested a review from a team as a code owner April 4, 2025 17:17
@ydah ydah force-pushed the new-change-without-expect branch from ae49a4e to 07cb1d9 Compare April 4, 2025 17:19
'without chains' do
expect_no_offenses(<<~RUBY)
it 'changes the count' do
change(Counter, :count)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn’t it register an offence?

(send
{
(block (send nil? :expect ...) ...)
(send nil? :expect ...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we match the non-block form here?


MSG = 'Use `change` matcher within an `expect` block.'
RESTRICT_ON_SEND = [:change].freeze
SINGLE_RESTRICTED_METHODS = %i[by by_at_least by_at_most from].freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# expect { subject }.to change(Counter, :count).by(1)
# end
#
class ChangeWithoutExpect < RuboCop::Cop::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this would detect a number of offences, wouldn’t it make more sense to make a more broad cop that would detect all built-in block matchers? “output”, “raise_error”, …

# expect { subject }.to change(Counter, :count).by(1)
# end
#
class ChangeWithoutExpect < RuboCop::Cop::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why only block matchers?

end
RUBY
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it behave with a case with two statements? Like “subject; change(:foo, :bar)”?

end
RUBY
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it the matcher is assigned to a variable, and then used multiple times?
“m = change { sum }
expect { foo }.to m
expect { bar }.to m

This is a legit RSpec construct, and should work fine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants