1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Fix callback handling from implicit traits

This commit is contained in:
Joshua Clayton 2011-10-28 23:01:50 -04:00
parent aee300aa90
commit 0124d42bf1
5 changed files with 26 additions and 16 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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