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

Cop idea: force ordering in factories

Open Darhazer opened this issue 3 years ago • 1 comments

Given a factory bot definition, it would be nice to force some consistent structure:

  • alphabetical attribute ordering. Note that we should recognise FactoryBot methods (transient, association, trait, etc.)
  • possibly alphabetical trait ordering
  • General grouping, similar to class structure - e.g. transients in the beginning, followed by associations, followed by attributes, followed by traits and factories

Those could be separate cops of course

Darhazer avatar Sep 23 '22 12:09 Darhazer

I'm on the fence regarding ordering, sometimes it's interleaved for better visibility:

role { :generic } # attribute
trait(:admin) do
  role { :admin }
end

tweaked_at { Time.current }

Speaking more broadly, and you know what I'm talking about by codename Ferrari, factories are the hottest pain point of testing. It's a shame there's no such thing as a factory_bot-style-guide. cc @AlexVKO. I'm always triggered with bad memories when seeing dreaded factories with multiple after(:create) { create(:assoc, ...) }.

pirj avatar Oct 15 '22 13:10 pirj