Change creation strategy in FactoryBot
I found out that's possible to change the FactoryBot strategy by invoking the to_create method inside the factory
definition.
We had to do that to make factory bot linting to work on a factory that acts like an enum. So we did something like this:
FactoryBot.define do
factory :role do
to_create do |instance|
instance.attributes = instance.class
.create_with(instance.attributes)
.find_or_create_by(slug: instance.slug)
.attributes
instance.instance_variable_set(:@new_record, false)
end
end
end
The said part here is that FactoryBot expects us to mutate that instance
in order to work.