rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

How to extend `RSpec/FactoryBot/CreateList` ?

Open leoarnold opened this issue 3 years ago • 4 comments

FactoryBot actually has a few more list methods than just create_list:

3.0.2 :006 > pp FactoryBot::Syntax::Methods.instance_methods.sort
[:attributes_for,
 :attributes_for_list,
 :attributes_for_pair,
 :build,
 :build_list,
 :build_pair,
 :build_stubbed,
 :build_stubbed_list,
 :build_stubbed_pair,
 :create,
 :create_list,
 :create_pair,
 :generate,
 :generate_list,
 :null,
 :null_list,
 :null_pair]

I would like to add these, but then the cop name CreateList does not fit any more. Maybe MultipleInstances would be more fitting.

Can I do that? How do I inform RuboCop about a renamed cop?

leoarnold avatar Nov 26 '21 13:11 leoarnold

If you consider a pair to be a list of two records, it's not necessary to rename the cop.

How do I inform RuboCop about a renamed cop?

Example: https://github.com/rubocop/rubocop/blob/master/config/obsoletion.yml

The enforced style is create_list. Unless you plan to also extend the cop to suggest/correct to _pair, this can be left intact, too.

In any case - a PR is welcome!

pirj avatar Nov 26 '21 13:11 pirj

@pirj Thanks. PR to follow.

leoarnold avatar Nov 26 '21 13:11 leoarnold

The CreateList checks for n.times { } usage. build_list was ignored on purpose, as nobody would really do n.times { build(...) } since this code has no side effect. Same got for attributes_for and build_stubbed. So if you are to extend the cop for other constructs, I guess it should be extended also for other patterns (like methods being used inside .each / .map block)

Darhazer avatar Nov 26 '21 16:11 Darhazer

Ping

pirj avatar Jan 11 '22 16:01 pirj