factory definition block and the
FactoryBot.define block, you have
access to the
callback methods. This allow you to hook
into parts of the build strategies.
factory definition block, these callbacks are scoped to just that
factory. Within a
FactoryBot.define block, they are global to all factories.
callback method allows you to hook into any factory_bot callback by
name. The pre-defined names, as seen in the build strategies reference, are
This method takes a splat of names, and a block. It invokes the block any time
one of the names is activated. The block can be anything that responds to
This block takes two arguments: the instance of the factory, and the factory_bot context. The context holds transient attributes.
The same callback name can be hooked into multiple times. Every block is run, in the order it was defined. Callbacks are inherited from their parents; the parents' callbacks are run first.
before methods add some nice syntax to
after(:create) do |user, context| user.post_first_article(context.article) end callback(:after_create) do |user, context| user.post_first_article(context.article) end