Ensure that callbacks are triggered once for implicit traits

Closes #393
This commit is contained in:
Joshua Clayton 2012-06-19 22:25:46 -04:00
parent 4a16f72364
commit 8f3cd52e0d
3 changed files with 25 additions and 5 deletions

View File

@ -21,7 +21,7 @@ module FactoryGirl
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

View File

@ -645,3 +645,27 @@ describe "nested implicit traits" do
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

View File

@ -25,10 +25,6 @@ describe FactoryGirl::DeclarationList, "#attributes" do
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