Skip to content

Fix an error RSpec/ChangeByZero cop when without expect block #2071

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

Merged
merged 1 commit into from
Apr 5, 2025

Conversation

lee266
Copy link

@lee266 lee266 commented Apr 4, 2025

issue number: #2069

Replace this text with a summary of the changes in your PR. The more detailed you are, the better.


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.
    • No changes needed
  • 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).

@lee266 lee266 requested a review from a team as a code owner April 4, 2025 11:04
@lee266 lee266 force-pushed the bugfix-change-by-zero branch 2 times, most recently from a05b88a to d1eb4af Compare April 4, 2025 12:03

expect_no_offenses(<<~RUBY)
it do
subject; change(Foo, :bar).by(0)
Copy link
Member

Choose a reason for hiding this comment

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

I would very much rather this cop to raise an exception than to swallow such a spec.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, that example might be unnecessary. However, it makes sense that code like change(foo, :bar).by(0) doesn't produce an error, so perhaps we could simply remove it from the test examples.

@ydah
Copy link
Member

ydah commented Apr 4, 2025

Could you rebase to the latest master branch?

@lee266 lee266 force-pushed the bugfix-change-by-zero branch from d1eb4af to e2953f5 Compare April 4, 2025 14:37
@ydah
Copy link
Member

ydah commented Apr 4, 2025

Thank you. Could you squashed related commits together?

@lee266 lee266 force-pushed the bugfix-change-by-zero branch from e2953f5 to ce811c7 Compare April 4, 2025 14:48
@lee266 lee266 force-pushed the bugfix-change-by-zero branch from ce811c7 to 9aa4b8a Compare April 4, 2025 15:35
@lee266 lee266 force-pushed the bugfix-change-by-zero branch from 9aa4b8a to 8c4429e Compare April 4, 2025 15:48
@lee266 lee266 changed the title Fix RSpec/ChangeByZero cop to handle invalid change matcher usage Fix an error RSpec/ChangeByZero cop when without expect block Apr 4, 2025
Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

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

Thank you❤️

@ydah
Copy link
Member

ydah commented Apr 4, 2025

@pirj If your concerns have now been resolved, merge this PR.

@pirj
Copy link
Member

pirj commented Apr 4, 2025

Again. I would rather have this current error and let users know about such a mistake in their specs than to swallow it.

Is there another cop that would catch this?

Presently, this cop’s deficiency alerts users in a way, and prevents them from writing specs with false negatives.

I’m not convinced that fixing this error is an improvement when there’s no other cop to catch this.

I clearly understand that this only “detects” such cases with the “change” matcher, and others are left out.

@lee266
Copy link
Author

lee266 commented Apr 4, 2025

@pirj
The RSpec/NoExpectationExample cop is designed to detect examples that do not contain proper expectations. In fact, it already flags cases like the following:

# spec/sample_spec.rb:13:3: C: RSpec/NoExpectationExample: No expectation found in this example.
  it { change(numbers, :size).by(0) }
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Given this, wouldn't it be more appropriate to let RSpec/NoExpectationExample handle such cases rather than adding this handling to RSpec/ChangeByZero?
Since RSpec/NoExpectationExample is already responsible for detecting examples that lack assertions, it seems more natural and preferable from a responsibility separation perspective to rely on it for these cases.

I would appreciate your thoughts on this.

ydah added a commit that referenced this pull request Apr 4, 2025
@ydah ydah mentioned this pull request Apr 4, 2025
13 tasks
ydah added a commit that referenced this pull request Apr 4, 2025
ydah added a commit that referenced this pull request Apr 4, 2025
@ydah
Copy link
Member

ydah commented Apr 4, 2025

I created RSpec/ChangeWithoutExpect to try it out

@pirj
Copy link
Member

pirj commented Apr 5, 2025

@lee266 This is reasonable. I apologise to ask this, can’t check this as I’m away from my computer. Does it also detect the case with two statements?

it { subject; change(numbers, :size).by(0) }

@lee266
Copy link
Author

lee266 commented Apr 5, 2025

@pirj
Yes, it does.

spec/sample_spec.rb:10:3: C: RSpec/NoExpectationExample: No expectation found in this example.
  it { subject; change(numbers, :size).by(0) }
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Sorry for the confusion, and thank you for the fix! 🙌

@pirj pirj merged commit c25abf4 into rubocop:master Apr 5, 2025
27 checks passed
@lee266
Copy link
Author

lee266 commented Apr 5, 2025

@pirj Also, thank you for sharing your thoughts — I appreciate your input.
@ydah. Thank you for the quick follow-up. I appreciate it!

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.

3 participants