Multiple callbacks
You can also define multiple types of callbacks on the same factory:
factory :user do
after(:build) { |user| do_something_to(user) }
after(:create) { |user| do_something_else_to(user) }
end
Factories can also define any number of the same kind of callback. These callbacks will be executed in the order they are specified:
factory :user do
after(:create) { this_runs_first }
after(:create) { then_this }
end
Calling create
will invoke both after_build
and after_create
callbacks.
Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
Multiple callbacks can be assigned to run a block; this is useful when building various strategies that run the same code (since there are no callbacks that are shared across all strategies).
factory :user do
callback(:after_stub, :before_create) { do_something }
after(:stub, :create) { do_something_else }
before(:create, :custom) { do_a_third_thing }
end