1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Add ability to pass traits at runtime to (build|create)_list

This commit is contained in:
Simone Carletti 2012-02-07 16:13:27 +01:00 committed by Joshua Clayton
parent 6e7e4ee5b8
commit 6482a6477f
2 changed files with 36 additions and 8 deletions

View file

@ -90,14 +90,15 @@ module FactoryGirl
# The name of the factory to be used. # The name of the factory to be used.
# * amount: +Integer+ # * amount: +Integer+
# number of instances to be built. # number of instances to be built.
# * overrides: +Hash+ # * traits_and_overrides: +Array+
# Attributes to overwrite for each instance. # [+*Array+] Traits to be applied
# [+Hash+] Attributes to overwrite for this instance.
# #
# Returns: +Array+ # Returns: +Array+
# An array of instances of the class this factory generates, with generated attributes # An array of instances of the class this factory generates, with generated attributes
# assigned. # assigned.
def build_list(name, amount, overrides = {}) def build_list(name, amount, *traits_and_overrides)
amount.times.map { build(name, overrides) } amount.times.map { build(name, *traits_and_overrides) }
end end
# Creates and returns multiple instances from this factory as an array. Attributes can be # Creates and returns multiple instances from this factory as an array. Attributes can be
@ -108,14 +109,15 @@ module FactoryGirl
# The name of the factory to be used. # The name of the factory to be used.
# * amount: +Integer+ # * amount: +Integer+
# number of instances to be created. # number of instances to be created.
# * overrides: +Hash+ # * traits_and_overrides: +Array+
# Attributes to overwrite for each instance. # [+*Array+] Traits to be applied
# [+Hash+] Attributes to overwrite for this instance.
# #
# Returns: +Array+ # Returns: +Array+
# An array of instances of the class this factory generates, with generated attributes # An array of instances of the class this factory generates, with generated attributes
# assigned. # assigned.
def create_list(name, amount, overrides = {}) def create_list(name, amount, *traits_and_overrides)
amount.times.map { create(name, overrides) } amount.times.map { create(name, *traits_and_overrides) }
end end
# Generates and returns the next value in a sequence. # Generates and returns the next value in a sequence.

View file

@ -262,6 +262,32 @@ describe "traits added via proxy" do
its(:admin) { should be_true } its(:admin) { should be_true }
its(:name) { should == "Jack" } its(:name) { should == "Jack" }
end end
context "adding traits in create_list" do
subject { FactoryGirl.create_list(:user, 2, :admin, :great, :name => "Joe") }
its(:length) { should == 2 }
it "creates all the records" do
subject.each do |record|
record.admin.should be_true
record.name.should == "JOE"
end
end
end
context "adding traits in build_list" do
subject { FactoryGirl.build_list(:user, 2, :admin, :great, :name => "Joe") }
its(:length) { should == 2 }
it "builds all the records" do
subject.each do |record|
record.admin.should be_true
record.name.should == "Joe"
end
end
end
end end
describe "traits and dynamic attributes that are applied simultaneously" do describe "traits and dynamic attributes that are applied simultaneously" do