How does FG track named associations in it's step definitions?

21 views
Skip to first unread message

Chris Bloom

unread,
May 8, 2012, 11:52:06 AM5/8/12
to factory_girl
I've seen this in examples but I just wanted to understand better how
it works. This is a contrived example based off of FG's own functional
tests:

Scenario: create post with and without a category association
Given the following users exist:
| ID | Name |
| 123 | Joe |
And the following posts exist:
| Title | Author | Category |
| a title | Name: Joe | Name: programming |
| a title without a category | Name: Bob | |

* How does FG know that "Joe" already exists but "Bob" needs to be
created?
* Once "Bob" is created, can I reference it later the same way I do
"Joe"
* Does it require that the named attribute have a unique constraint on
it?
* If not, what would happen if you had two users both with the name
"Joe"?
* Finally, is there a way to specify an association to an existing
object based on two attributes, like first name AND last name?

Joe Ferris

unread,
May 9, 2012, 9:39:52 AM5/9/12
to factor...@googlegroups.com
Hey Chris,



As you can see, it looks for an existing record by calling #first with a conditions hash. If it doesn't find one, it calls FactoryGirl.create with the specified attribute and value. Because it looks for the first row that matches, having two matching rows means the database gets to decide which row to return. In some databases that's the first one that's created, but it's essentially undefined. You should make sure to only have one matching row.

You can specify an association by using the syntax `Attribute: Value` in a cucumber table. This will only work for attributes that have been defined as associations in your factories. There isn't currently a way to specify an association based on multiple attributes.

All that being said, we've largely stopped using the factory_girl step definitions in new projects, favoring higher level step definitions instead. The factory_girl step definitions can be harmful for the same reasons that web_steps weren't good. The author of Cucumber wrote about this in detail here: http://aslakhellesoy.com/post/11055981222/the-training-wheels-came-off

-Joe
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:

You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group at

Chris Bloom

unread,
May 17, 2012, 8:14:40 PM5/17/12
to factor...@googlegroups.com
Thanks for the detailed response, Joe. That's interesting to know that you're not using them internally anymore. I can see why you might compare them to the old web_steps file in Cucumber. Personally, I still find them incredibly useful and they make for very concise scenario steps. The few times I've been tripped up by using them are far outweighed by all of the steps I didn't have to write. I hope you continue to include them.
Reply all
Reply to author
Forward
0 new messages