rubocop / rubocop-rspec

Code style checking for RSpec files.
https://docs.rubocop.org/rubocop-rspec
MIT License
792 stars 272 forks source link

Error with RSpec/ChangeByZero #1921

Closed ryan-mcneil closed 3 weeks ago

ryan-mcneil commented 3 weeks ago

Hi,

While running Rubocop (1.64.1) w/ rubocop-rspec (3.0.1), I'm getting the following error:

5 errors occurred:
An error occurred while RSpec/ChangeByZero cop was inspecting /home/runner/work/platform-atlas/platform-atlas/spec/controllers/product_directory/codeowners_controller_spec.rb:102:8.
An error occurred while RSpec/ChangeByZero cop was inspecting /home/runner/work/platform-atlas/platform-atlas/spec/controllers/product_directory/codeowners_controller_spec.rb:135:8.
An error occurred while RSpec/ChangeByZero cop was inspecting /home/runner/work/platform-atlas/platform-atlas/spec/controllers/product_directory/products_controller_spec.rb:124:8.
An error occurred while RSpec/ChangeByZero cop was inspecting /home/runner/work/platform-atlas/platform-atlas/spec/controllers/product_directory/teams_controller_spec.rb:135:8.
An error occurred while RSpec/ChangeByZero cop was inspecting /home/runner/work/platform-atlas/platform-atlas/spec/controllers/product_directory/teams_controller_spec.rb:312:8.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Inspecting [16](https://github.com/department-of-veterans-affairs/platform-atlas/actions/runs/9489870762/job/26152087527#step:6:17)4 files
....................................................................................................................................................................

164 files inspected, no offenses detected
Mention the following information in the issue report:
1.64.1 (using Parser 3.3.3.0, rubocop-ast 1.31.3, running on ruby 3.2.4) [arm64-darwin23]

Unfortunately this repo is currently private, but the following lines, respectively, caused the 5 errors: change(ProductDirectory::Codeowner, :count).by(0) X 2 change(ProductDirectory::Product, :count).by(0) change(ProductDirectory::Team, :count).by(0) X 2

Hopefully that's all you need to investigate - Thank you!!

ydah commented 3 weeks ago

@ryan-mcneil Thank you for your report. I could not reproduce it with just the code you provided. Can you give us the code to reproduce it?

Darhazer commented 3 weeks ago

If you can’t provide a full example, perhaps you can run rubocop with -d flag and provide the error and rubocop backtrace?

ryan-mcneil commented 3 weeks ago

Hey, apologies for the delay.

So, I dug in a bit to see how I could provide enough context code to get it to work properly. It wasn't my code, so I had to figure out what they were trying to do. Turns out, they were using that line incorrectly. They were using change(ProductDirectory::Codeowner, :count).by(0) AS an expectation, vs being used in conjunction with an expectation expect { some_action }.to change(ProductDirectory::Codeowner, :count).by(0).

Fixing that, the errors actually went a way. So this may not be an edge case you need to fix. But perhaps an idea for a future cop could be checking for orphaned, useless matchers 🙃 😆

Sorry for the false alarm!

pirj commented 3 weeks ago

Thanks for the feedback! We’ve just merged a new cop that helps with similar situations, but bot exactly what you describe.