rubocop-factory_bot
rubocop-factory_bot copied to clipboard
Cop idea: enforces usage of create_pair
Hello team,
I have a simple cop proposal, the idea is to enforce the usage of create_pair instead of create_list of 2.
# @example
# # bad
# create_list(:foo, 2)
#
# # good
# create_pair(:foo)
#
Let me know if it make sense to you. Thanks.
Hi!
Thanks for the suggestion.
Personally, I'm quite conservative, and can't remember using create_pair deliberately in practice.
"Pair" sounds like those two are somehow related, apart from being in the same array, like a two-element tuple.
Are there practical uses for that where it would make sense, like create_pair(:boot), or create_pair(:ice_skater).
If e.g. there's a spec with three examples, one with a single-element array, another with a pair, and yet another with a three-element array, I would dismiss the idea to use another method for the second one. Not to say, we recently started tolerating single-element arrays (let(:luggage) { [create(:bag)] }).
There are certain methods, like Rails Array#second, which might sometimes increase readability, but I wouldn't agree to replace all array[1] calls with array.second. I'm uncertain if it's possible to come up with a rule to use one vs another.