FactoryGirl and after_initalize
I use FactoryGirl for testing, and found a major problem and solution last week.
The Problem
We use Rails after_initialization
callbacks in object construction, but Rails was acting as if the parameters that we passed to FactoryGirl build
or create
calls weren’t there.
Well, they weren’t, at least not at object initialization..
What’s Going On
Turns out that FactoryGirl’s default behavior is to new
or create
the object with no arguments, and then set the properties after
object initialization. Hey, guess what? That’s too late for use in after_initialization
callbacks.
Solution
You can customize a Factory’s constructor to pass certain (or all) parameters to a particular factory. I had the best luck using the big hammer of
initialize_with { new(attributes) }
Your results may vary.
Thanks to @typed for pointing this out!