0

While I usually prefer Factory Girl, I am in the need of using existing fixtures to test an application I am working on.

The problem is that I need to use test-dependent sets of fixture, ie I have multiple sets for the same model that need to be loaded depending on which test I am running.

In the old days of rails 0.x there were a couple of plugins to do that. How do I accomplish the same with Rails 3+?

Thanks in advance

Topo
  • 1,364
  • 10
  • 22
  • 1
    This appears to be a duplicate of: http://stackoverflow.com/questions/7894781/multiple-fixture-sets-in-rails – David J. Jul 17 '12 at 03:38

2 Answers2

0

You can create some script file which call your factories to create records. Then require the scripts in your tests.

Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52
  • It would be great if you could provide such scripts as an example to start with. – Topo Jul 18 '12 at 12:09
  • Thought this example would be obvious. FactoryGirl.create(:model1) FactoryGirl.create(:model2, :attr1 => FactoryGirl.create(:model3)) – Marlin Pierce Jul 23 '12 at 12:37
0

You say you are a fan of Factory Girl, so I don't have to sell you on how awesome it is.

Why not convert those fixtures to factories? Convert Fixtures into Factory Girl in Rails should be of assistance.

Once you have your data as factories in Factory Girl, I recommend that you take advantage of its inheritance features as shown in https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md:

You can easily create multiple factories for the same class without repeating common attributes by nesting factories:

factory :post do
  title "A title"

  factory :approved_post do
    approved true
  end
end

approved_post = FactoryGirl.create(:approved_post)
approved_post.title    # => "A title"
approved_post.approved # => true

You can also assign the parent explicitly:

factory :post do
  title "A title"
end

factory :approved_post, parent: :post do
  approved true
end
Community
  • 1
  • 1
David J.
  • 31,569
  • 22
  • 122
  • 174
  • 1
    Hi James and tanks for the time spent in drafting the answer. Nevertheless, the question is how to load test-specific fixtures and not how to use Factory Girl to replace the fixtures. As said, I appreciated the advantages of Factory Girl in several projects but lately I prefer to use the fixtures. – Topo Jul 18 '12 at 12:07