Ron Garrity
Ron Garrity

Reputation: 1463

When testing in Rails, do I use seeds or factory girl or both?

I have a Store model w/ several foreign keys. I have state_id, country_id, currency_id, timezone_id, etc... Should I create a factory for each one of these? And then in Store's factory do something like:

a.state State.where(:abbreviation => "MA").first || Factory(:state, :abbreviation => "MA")

Or should I just seed this data?

Upvotes: 1

Views: 294

Answers (2)

thiagofm
thiagofm

Reputation: 5883

Use factory girl, it already does what rake seed do.

If you are asking me that, for this special case(implying you are already using factory girl), you should use rake seed. I give you a even bigger NO. That breaks your organization with your tests.

Upvotes: 1

Marlin Pierce
Marlin Pierce

Reputation: 10089

If you use the rake seed task, it will only add it to the database. However, many things, like the db:test:prepare task, will delete all the records from the database, so the seed task will not be effective.

Your factory approach looks good, but I suggest you put the code in a pair of braces.

a.state { State.where(:abbreviation => "MA").first || Factory(:state, :abbreviation => "MA") }

This will re-run every time the factory is called, and will prevent creating duplicates in your database.

Upvotes: 3

Related Questions