Skip to content

Commit

Permalink
Add ability to pass traits at runtime to (build|create)_list
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos authored and joshuaclayton committed Feb 8, 2012
1 parent 6e7e4ee commit 6482a64
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/factory_girl/syntax/methods.rb
Expand Up @@ -90,14 +90,15 @@ def build_stubbed(name, *traits_and_overrides, &block)
# 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
Expand All @@ -108,14 +109,15 @@ def build_list(name, amount, overrides = {})
# 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.
Expand Down
26 changes: 26 additions & 0 deletions spec/acceptance/traits_spec.rb
Expand Up @@ -262,6 +262,32 @@
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
Expand Down

0 comments on commit 6482a64

Please sign in to comment.