rubocop-factory_bot
rubocop-factory_bot copied to clipboard
Cop idea: force ordering in factories
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
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, ...) }.