What is the purpose of transient do
in FactoryBot factories?
I\'ve seen a lot of factories that begin with something like below.
factory :c
Transient attributes are essentially variables local to the factory that do not persist into the created object.
I have seen two main uses of transient attributes:
You could, of course, use these transient attributes for anything else you need to code during the object creation.
factory :car do
transient do
accident_count 0
unsold false
end
owner unsold ? 'new inventory' : nil
after(:create) do |car, evaluator|
create_list(:police_report, evaluator.accident_count, vehicle: car)
end
end
This lets your test express a concept (similar to a trait), without knowing anything about the implementation:
FactoryBot.create(:car, make: 'Saturn', accident_count: 3)
FactoryBot.create(:car, make: 'Toyota', unsold: true)
IMO, I would stick with traits when they work (e.g. unsold, above). But when you need to pass a non-model value (e.g. accident_count), transient attributes are the way to go.