FactoryGirl create_list with different values

I am trying to use FactoryGirl to create a list of items, but I need the items not to be in order. This is what I have, but I would like him 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 elements with:

  FactoryGirl.create_list(:thing, ordering: 3 ) 

but I wanted to create the elements to check their order, and they will create all of them in order, since I have the sequence set in the factory.

+5
source share
1 answer

How about this?

 before do (1..3).to_a.shuffle.each do |order| FactoryGirl.create(:thing, ordering: order) end end 
+1
source

Source: https://habr.com/ru/post/1207166/


All Articles