Writing Good Factories

05 Dec 2009

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.

1. Should be able to loop

10.times { Factory(:user) }

2. No associations in the base Factory

Factory(:user) and Factory(:user_with_items)

3. 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.