rubocop-rspec: CreateList is not a safe cop
Description
rubocop autocorrect this :
10.times { create(:user, age: rand(10..0)) }
to
create_list(:user, 10, age: rand(10..0))
both obviously do not have the same output as in the corrected version rand is only evaluated once and will attribute its result to all users.
We can “prevent” this behaviour by using the n like 10.times { |n| …}but this is just a corner case of multiple possibilities wherex.times` is just a legit call
Expected behavior
~this should not be a safe cop~
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 20 (20 by maintainers)
Commits related to this issue
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
- [Fix #1087] Ignore CreateList offense on method call detection — committed to ngouy/rubocop-rspec by ngouy 4 years ago
@pirj PR in draft √
Sounds great. Please don’t hesitate to send the PR early and asking questions, writing cops is tricky at first.
As I’ve mentioned somewhere above, this cop is far from perfection. We’ll keep it safe, and this is what matters. I can surely see a different FB cop that would advise using
author: user.Personally, especially after https://github.com/thoughtbot/factory_bot/issues/1444 pitfall, I strongly prefer
n.timessyntax.What bothers me with this, is that we will kick our a lot (if not most) of true positive offences.
Indeed it is really common to do:
Your solution seems easier to apply. I’m ok to go with that I just want to point out that this will probably kill a lot “it happens really often in real-life use cases” true positive detections