Skip to content

Add explanation for ExpectChange unsafe autocorrection #2061

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

Conversation

lovro-bikic
Copy link
Contributor

@lovro-bikic lovro-bikic commented Mar 14, 2025

#1178 marked RSpec/ExpectChange's autocorrection as unsafe, but it didn't update the docs. This PR adds an explanation for the cop's unsafe autocorrection.


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).

@lovro-bikic lovro-bikic requested a review from a team as a code owner March 14, 2025 19:16
@lovro-bikic lovro-bikic force-pushed the provide-safety-reason-for-expect-change branch 2 times, most recently from 972aed1 to e8d554b Compare March 14, 2025 19:19
@lovro-bikic lovro-bikic force-pushed the provide-safety-reason-for-expect-change branch from e8d554b to 465d0c4 Compare March 28, 2025 08:07
# @safety
# Autocorrection is unsafe because `block` style evaluates the object
# twice (before and after evaluating `expect` block), whereas
# `method_call` style evaluates the object only once.
Copy link
Member

Choose a reason for hiding this comment

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

What do you mean by “object” here? Statement or a series of statements inside the block?

Copy link
Member

Choose a reason for hiding this comment

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

After looking at the original PR, it’s clear to me. Nice catch, and thanks for marking the cop, it’s totally justified.

Can you please provide a bit more context in safety docs to make it obvious for the reader?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I understand #1174 correctly, the issue is that the method_call style calls message twice on the same receiver, so to speak. But with the block style, the entire block is evaluated twice – including instantiating two different receiver objects.

In #1174, the #time method is being called twice in each example, which confused me at first.

Copy link
Contributor Author

@lovro-bikic lovro-bikic Mar 28, 2025

Choose a reason for hiding this comment

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

What do you mean by “object” here? Statement or a series of statements inside the block?

I followed the terminology used in the previous section ("Enforces either passing object and attribute as...") which I think was in turn borrowed from the example in RSpec docs:

expect { do_something }.to change(object, :attribute)

Instead of object/attribute, more precise terms would be receiver/message, but I'm not sure if that would be better understood in these docs?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting that rspec.info uses object/attribute, while the implementation uses receiver/message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I gave these docs a redo, and also added an example to Safety section to hopefully make it clearer. Can you have another look?

@lovro-bikic lovro-bikic force-pushed the provide-safety-reason-for-expect-change branch from 465d0c4 to da995f3 Compare March 28, 2025 21:25
@lovro-bikic lovro-bikic force-pushed the provide-safety-reason-for-expect-change branch from da995f3 to 1d47c3d Compare March 28, 2025 21:28
Copy link
Collaborator

@bquorning bquorning 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 ❤️ The explanation is so much clearer now.

@bquorning bquorning merged commit e85969d into rubocop:master Mar 28, 2025
27 checks passed
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❤️

@lovro-bikic lovro-bikic deleted the provide-safety-reason-for-expect-change branch March 28, 2025 22:01
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.

4 participants