05 December 2009
Writing Good Factories
I really wanted to remember what Pratik had to say about writing good factories in his Rails Summit talk so I’m putting it up here as a reference.
- Should be able to loop
10.times { Factory(:user) }
- No associations in the base Factory
Factory(:user) and Factory(:user_with_items)
- Should pass validations
I am thinking that rule #2 can be broken in order to achieve #3. Sometimes an object has to have a parent. I think it may be more accurate to say “No has many associations in the base Factory”, but I’m still giving it some thought.