Description
Problem this feature will solve
Like many, I use the same factory with multiple strategies, based on the individual test.
Critically, I want the same code executed on completion, regardless of the strategy used.
With :create also calling :build I need to determine in the :after_build callback if I should execute the code or not.
The strategy can be found with context.instance_variable_get(:@build_strategy).to_sym
, but that required a degree of familiarity with the code that most users won't have.
Desired solution
A helper method for the context would simplify the whole process:
factory :user do
after(:build) { |user, context| run_this_code() if context.strategy.build?}
after(:stub) { |user, context| run_this_code() }
after(:create) { |user, context| run_this_code() }
end
Additional context
I'll do a pull request for this feature.