Shouldn't factory_girl respect specified strategy for associated objects?

127 views
Skip to first unread message

Deivid Rodriguez

unread,
Apr 5, 2013, 7:30:11 AM4/5/13
to factor...@googlegroups.com
I'm creating an object like this

FactoryGirl.build(:user)

but associated objects get created and saved in database. I would expect everything to be "created" if I specify FactoryGirl.create(:user) and everything to be just "built" if I say FactoryGirl.build(:user) unless I say something else inside the factory. Maybe I could get this to work as I expect using callbacks, but... Shouldn't this be a default? Am I missing something?

Congratulations for this useful gem and I'm sorry if this is discussed somewhere else... I couldn't find it.


Josh Clayton

unread,
Apr 5, 2013, 9:12:39 AM4/5/13
to factor...@googlegroups.com
Hi Deivid,

If you build a record, all associations will be created for you by default. This is due to how ActiveRecord handles assignment of associations and how objects end up getting persisted in the database, but there is a way to override it. If you check out https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations, there's a bit of code demonstrating how to override the build strategy for one specific association, if you absolutely need to do so. If your objects are simple and you're writing simple unit tests, you may want to skip factory_girl altogether for those (oftentimes I've found unit tests don't actually need records that've been persisted in the database or set up with a ton of attributes from my factories) and just use User.new(attr_to_test: 123, dependent_attr_to_test: 456).

Best of luck!

Deivid Rodriguez

unread,
Apr 5, 2013, 9:32:28 AM4/5/13
to factor...@googlegroups.com
Thanks for the quick reply! :)

I see I can achieve what I want by specifying "association my_association, strategy: build" inside the factory, and overriding that behaviour if I need it using FactoryGirl.create. Still I don't find it very intuitive: I wouldn't expect the factory to persist anything unless I tell it to do so.

Josh Clayton

unread,
Apr 5, 2013, 10:12:50 AM4/5/13
to factor...@googlegroups.com
While I understand it's not intuitive, it's actually more often than not what needs to happen. Oftentimes, developers will specify validations requiring associated data, which would break if you try to call #save on a model built with FactoryGirl. Additionally, I mentioned before that AR handles associated data very oddly and over the course of a few years, we've found that creating associated records and not saving the main factory is actually more intuitive, but you'll have to take my word on that :-)

You could use build_stubbed, which will use build_stubbed for all associated records too, but it doesn't allow any database interaction.

Deivid Rodriguez

unread,
Apr 5, 2013, 10:24:12 AM4/5/13
to factor...@googlegroups.com
Alright, I'll take your word on that for now!

Thanks a lot.
Reply all
Reply to author
Forward
0 new messages