FactoryBot to build list of objects with trait
Doesn't this work? It should...
FactoryBot.build_list(:user, 5, :with_photo)
Reference
FactoryBot - Building or Creating Multiple Records
Note: FactoryBot was previously called FactoryGirl
You can also pass multiple traits to create_list
and build_list
example;
factory :user do
name { "Friendly User" }
trait :male do
name { "John Doe" }
gender { "Male" }
end
trait :admin do
admin { true }
end
end
# creates 3 admin users with gender "Male" and name "Jon Snow" using the admin and male trait
build_list(:user, 3, :admin, :male, name: "Jon Snow")
create_list(:user, 3, :admin, :male, name: "Jon Snow")
Just make sure the traits comes after the number of records you wish to create, the last argument is a hash that would override the record attribute.
More on traits on the official docs