diff --git a/lib/factory_girl/declaration/implicit.rb b/lib/factory_girl/declaration/implicit.rb index 3e7fc43..482aef9 100644 --- a/lib/factory_girl/declaration/implicit.rb +++ b/lib/factory_girl/declaration/implicit.rb @@ -23,7 +23,8 @@ module FactoryGirl elsif FactoryGirl.sequences.registered?(name) [Attribute::Sequence.new(name, name, @ignored)] else - @factory.trait_by_name(name).attributes.to_a + @factory.inherit_traits([name]) + [] end end end diff --git a/lib/factory_girl/definition.rb b/lib/factory_girl/definition.rb index 8f486b3..e4936fe 100644 --- a/lib/factory_girl/definition.rb +++ b/lib/factory_girl/definition.rb @@ -7,10 +7,19 @@ module FactoryGirl @callbacks = [] @defined_traits = [] @to_create = nil + @traits = [] end delegate :declare_attribute, :to => :attribute_list + def traits + @traits.reverse.map { |name| trait_by_name(name) } + end + + def inherit_traits(new_traits) + @traits += new_traits + end + def add_callback(callback) @callbacks << callback end @@ -27,12 +36,12 @@ module FactoryGirl @defined_traits << trait end + private + def trait_by_name(name) trait_for(name) || FactoryGirl.trait_by_name(name) end - private - def trait_for(name) defined_traits.detect {|trait| trait.name == name } end diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 0c19a67..a63ef61 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -10,14 +10,15 @@ module FactoryGirl @name = name.to_s.underscore.to_sym @parent = options[:parent] @aliases = options[:aliases] || [] - @traits = options[:traits] || [] @class_name = options[:class] @default_strategy = options[:default_strategy] @definition = Definition.new(@name) + + inherit_traits(options[:traits] || []) end delegate :add_callback, :declare_attribute, :to_create, :define_trait, - :defined_traits, :trait_by_name, :to => :@definition + :defined_traits, :traits, :inherit_traits, :to => :@definition def factory_name $stderr.puts "DEPRECATION WARNING: factory.factory_name is deprecated; use factory.name instead." @@ -128,10 +129,6 @@ module FactoryGirl end end - def traits - @traits.reverse.map { |name| trait_by_name(name) } - end - def parent if @parent FactoryGirl.factory_by_name(@parent) diff --git a/spec/acceptance/traits_spec.rb b/spec/acceptance/traits_spec.rb index 43461ec..b9284b8 100644 --- a/spec/acceptance/traits_spec.rb +++ b/spec/acceptance/traits_spec.rb @@ -197,6 +197,6 @@ describe "traits with callbacks" do context "when the factory has an implicit trait" do subject { FactoryGirl.create(:caps_user_implicit_trait) } - its(:name) { pending } + its(:name) { should == "JOHN" } end end diff --git a/spec/factory_girl/definition_spec.rb b/spec/factory_girl/definition_spec.rb index 63f25fb..ad5db4e 100644 --- a/spec/factory_girl/definition_spec.rb +++ b/spec/factory_girl/definition_spec.rb @@ -47,21 +47,24 @@ describe FactoryGirl::Definition, "#to_create" do end end -describe FactoryGirl::Definition, "#trait_by_name" do +describe FactoryGirl::Definition, "#traits" do let(:female_trait) { stub("female trait", :name => :female) } let(:admin_trait) { stub("admin trait", :name => :admin) } before do subject.define_trait(female_trait) + FactoryGirl.stubs(:trait_by_name => admin_trait) end - it "finds the correct trait if defined on the definition" do - subject.trait_by_name(:female).should == female_trait + its(:traits) { should be_empty } + + it "finds the correct traits after inheriting" do + subject.inherit_traits([:female]) + subject.traits.should == [female_trait] end it "looks for the trait on FactoryGirl" do - FactoryGirl.stubs(:trait_by_name => admin_trait) - subject.trait_by_name(:admin).should == admin_trait - FactoryGirl.should have_received(:trait_by_name).with(:admin) + subject.inherit_traits([:female, :admin]) + subject.traits.should == [admin_trait, female_trait] end end