diff --git a/lib/factory_girl.rb b/lib/factory_girl.rb index 25301a30d..e9775c3b3 100644 --- a/lib/factory_girl.rb +++ b/lib/factory_girl.rb @@ -6,6 +6,7 @@ require 'factory_girl/proxy/attributes_for' require 'factory_girl/proxy/stub' require 'factory_girl/registry' +require 'factory_girl/null_factory' require 'factory_girl/factory' require 'factory_girl/attribute' require 'factory_girl/attribute/static' diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 04d6cbb42..0c19a674d 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -29,7 +29,7 @@ def build_class #:nodoc: end def default_strategy #:nodoc: - @default_strategy || (parent && parent.default_strategy) || :create + @default_strategy || parent.default_strategy || :create end def allow_overrides @@ -89,17 +89,15 @@ def names end def compile - if parent - parent.defined_traits.each {|trait| define_trait(trait) } - parent.compile - end + parent.defined_traits.each {|trait| define_trait(trait) } + parent.compile attribute_list.ensure_compiled end protected def class_name #:nodoc: - @class_name || (parent && parent.class_name) || name + @class_name || parent.class_name || name end def attributes @@ -110,14 +108,12 @@ def attributes end list.apply_attribute_list(attribute_list) - list.apply_attribute_list(parent.attributes) if parent + list.apply_attribute_list(parent.attributes) end end def callbacks - [traits.map(&:callbacks), @definition.callbacks].tap do |result| - result.unshift(*parent.callbacks) if parent - end.flatten + [parent.callbacks, traits.map(&:callbacks), @definition.callbacks].flatten end private @@ -137,8 +133,11 @@ def traits end def parent - return unless @parent - FactoryGirl.factory_by_name(@parent) + if @parent + FactoryGirl.factory_by_name(@parent) + else + NullFactory.new + end end def attribute_list diff --git a/lib/factory_girl/null_factory.rb b/lib/factory_girl/null_factory.rb new file mode 100644 index 000000000..6e88cf7f7 --- /dev/null +++ b/lib/factory_girl/null_factory.rb @@ -0,0 +1,19 @@ +module FactoryGirl + class NullFactory + attr_reader :definition + + def initialize + @definition = Definition.new + end + + delegate :defined_traits, :callbacks, :to => :definition + + def compile; end + def default_strategy; end + def class_name; end + + def attributes + AttributeList.new + end + end +end diff --git a/spec/factory_girl/null_factory_spec.rb b/spec/factory_girl/null_factory_spec.rb new file mode 100644 index 000000000..a653581ce --- /dev/null +++ b/spec/factory_girl/null_factory_spec.rb @@ -0,0 +1,11 @@ +require "spec_helper" + +describe FactoryGirl::NullFactory do + it { should delegate(:defined_traits).to(:definition) } + it { should delegate(:callbacks).to(:definition) } + + its(:compile) { should be_nil } + its(:default_strategy) { should be_nil } + its(:class_name) { should be_nil } + its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) } +end