0

Just started using factory girl. If one has to save a factory into the db then one has to run FactoryGirl.create().

But is there any way to load a collection of factories into the database without having to call create on each of the factories?

If its of any help i'm using Ruby 1.9.3, Rails 3.2.8 and Factory Girl 4.1.

Phoenix
  • 192
  • 1
  • 8

1 Answers1

1

If you need a bunch of factories of different type, you'll simply have to create them individually. You can make this a bit less verbose by adding this to your test_helper.rb:

include FactoryGirl::Syntax::Methods

Then you can create factories like this:

create(:factory_name)

And of course, if you need a bunch of records of the same type, you can create them like this:

5.times { create(:factory_name) }

As mentioned in my comment, creating all your factories in one fell swoop on initialization of your test framework is counter-productive. The idea behind FactoryGirl is to provide just the data you need for each test case or context. This avoids dependencies between tests that tend to slip into a fixture-driven approach.

Thilo
  • 17,565
  • 5
  • 68
  • 84
  • Is there any other way of doing the same. For instance I have 20 unique factories, how would I go about creating all of them? – Phoenix Oct 30 '12 at 11:56
  • Unique in terms of model, or data? Maybe post your code, this is really a refactoring question. – Thilo Oct 30 '12 at 12:02
  • Unique in terms of data, right now i'm looking at factories for one model with no associations. – Phoenix Oct 30 '12 at 12:05
  • Something on the lines of what fixtures do, i.e., during initialization they are dumped into the database. I'm not sure whether this is the best thing to do considering the capabilities of factories, but just want to know if something on similar lines is possible with factories. Thx for the quick reply :) – Phoenix Oct 30 '12 at 12:11
  • You don't want to dump all your test data into the test DB on initialization - or if you do, just stay with fixtures. FactoryGirl is intended to create *just* the test data you need for any particular test or context. – Thilo Oct 30 '12 at 12:37
  • I was afraid of that, got a hint while trying to google for a solution. Thanks you for all the help. Can you updated you above comment to be part of the answer? – Phoenix Oct 30 '12 at 12:46