FactoryGirl create_list with different values
Asked Answered
T

1

6

I'm trying to user FactoryGirl to create a list of items, but I need the items to not be in order. This is what I have, but I would like it to be DRYer.

spec.rb

context "three out of order" do
  before do
    FactoryGirl.create(:thing, ordering: 3)
    FactoryGirl.create(:thing, ordering: 1)
    FactoryGirl.create(:thing, ordering: 2)
  end

   it "should sort the things in order" do
    expect(Thing.all.map(&:ordering)).to eq([1, 2, 3])
   end
end

I know that you can create multiple items with:

   FactoryGirl.create_list(:thing, ordering: 3 )

but I wanted to create items so it tested their order, and they would create all of them in order since I have sequence set up in the factory.

Thaliathalidomide answered 18/11, 2014 at 21:43 Comment(0)
C
1

What about this?

before do
  (1..3).to_a.shuffle.each do |order|
    FactoryGirl.create(:thing, ordering: order)
  end
end
Commit answered 18/11, 2014 at 21:57 Comment(1)
This is a classic case of solving the problem, but not answering the question. Obviously that could be done, it's very much like the original example, but the goal here is the user create_list and to keep it simpler. The question was not "How can I create three model with different names", but rather "Can I use create_list to create three models with different names", which unfortunately is not possible.Garnet

© 2022 - 2024 — McMap. All rights reserved.