Skip to content

Commit

Permalink
Ensure that callbacks are triggered once for implicit traits
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jun 20, 2012
1 parent 4a16f72 commit 8f3cd52
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/factory_girl/declaration_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def overridable
end

def attributes
AttributeList.new(@name).tap do |list|
@attributes ||= AttributeList.new(@name).tap do |list|
to_attributes.each do |attribute|
list.define_attribute(attribute)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/acceptance/traits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,27 @@ def initialize(name)
it_should_behave_like "assigning data from traits"
end
end

describe "implicit traits containing callbacks" do
before do
define_model("User", value: :integer)

FactoryGirl.define do
factory :user do
value 0

trait :trait_with_callback do
after(:build) {|user| user.value += 1 }
end

factory :user_with_trait_with_callback do
trait_with_callback
end
end
end
end

it "only runs the callback once" do
FactoryGirl.build(:user_with_trait_with_callback).value.should == 1
end
end
4 changes: 0 additions & 4 deletions spec/factory_girl/declaration_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
attribute_list.should have_received(:define_attribute).with(static_attribute_2)
attribute_list.should have_received(:define_attribute).with(dynamic_attribute_1)
end

it "creates a new attribute list upon every invocation" do
subject.attributes.should_not == subject.attributes
end
end

describe FactoryGirl::DeclarationList, "#declare_attribute" do
Expand Down

0 comments on commit 8f3cd52

Please sign in to comment.