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:
parent
6e7e4ee5b8
commit
6482a6477f
2 changed files with 36 additions and 8 deletions
|
@ -90,14 +90,15 @@ module FactoryGirl
|
|||
# The name of the factory to be used.
|
||||
# * amount: +Integer+
|
||||
# number of instances to be built.
|
||||
# * overrides: +Hash+
|
||||
# Attributes to overwrite for each instance.
|
||||
# * traits_and_overrides: +Array+
|
||||
# [+*Array+] Traits to be applied
|
||||
# [+Hash+] Attributes to overwrite for this instance.
|
||||
#
|
||||
# Returns: +Array+
|
||||
# An array of instances of the class this factory generates, with generated attributes
|
||||
# assigned.
|
||||
def build_list(name, amount, overrides = {})
|
||||
amount.times.map { build(name, overrides) }
|
||||
def build_list(name, amount, *traits_and_overrides)
|
||||
amount.times.map { build(name, *traits_and_overrides) }
|
||||
end
|
||||
|
||||
# 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.
|
||||
# * amount: +Integer+
|
||||
# number of instances to be created.
|
||||
# * overrides: +Hash+
|
||||
# Attributes to overwrite for each instance.
|
||||
# * traits_and_overrides: +Array+
|
||||
# [+*Array+] Traits to be applied
|
||||
# [+Hash+] Attributes to overwrite for this instance.
|
||||
#
|
||||
# Returns: +Array+
|
||||
# An array of instances of the class this factory generates, with generated attributes
|
||||
# assigned.
|
||||
def create_list(name, amount, overrides = {})
|
||||
amount.times.map { create(name, overrides) }
|
||||
def create_list(name, amount, *traits_and_overrides)
|
||||
amount.times.map { create(name, *traits_and_overrides) }
|
||||
end
|
||||
|
||||
# Generates and returns the next value in a sequence.
|
||||
|
|
|
@ -262,6 +262,32 @@ describe "traits added via proxy" do
|
|||
its(:admin) { should be_true }
|
||||
its(:name) { should == "Jack" }
|
||||
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
|
||||
|
||||
describe "traits and dynamic attributes that are applied simultaneously" do
|
||||
|
|
Loading…
Reference in a new issue