From 8f3b24a559aec25d058734bdd94fbbb774b89957 Mon Sep 17 00:00:00 2001 From: Nathan Sutton Date: Sat, 10 Oct 2009 00:23:57 -0500 Subject: [PATCH] Fixing inheritance of callbacks Since callbacks share the same names they wouldn't be inherited. I added a check to allow them to be inherited. --- lib/factory_girl/factory.rb | 2 +- spec/factory_girl/factory_spec.rb | 14 ++++++++++++++ spec/factory_girl/proxy/stub_spec.rb | 4 ++-- spec/integration_spec.rb | 10 ++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index a182f28..5b38d77 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -351,7 +351,7 @@ class Factory end def attribute_defined? (name) - !@attributes.detect {|attr| attr.name == name }.nil? + !@attributes.detect {|attr| attr.name == name && !attr.is_a?(Factory::Attribute::Callback) }.nil? end def assert_valid_options(options) diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index 5eaf6c4..46dccf3 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -458,6 +458,20 @@ describe Factory do child.attributes.size.should == 1 child.attributes.first.should be_kind_of(Factory::Attribute::Dynamic) end + + it "inherit all callbacks" do + Factory.define(:child, :parent => :object) do |f| + f.after_stub {|o| o.name = 'Stubby' } + end + + grandchild = Factory.define(:grandchild, :parent => :child) do |f| + f.after_stub {|o| o.name = "#{o.name} McStubby" } + end + + grandchild.attributes.size.should == 3 + grandchild.attributes.first.should be_kind_of(Factory::Attribute::Callback) + grandchild.attributes[1].should be_kind_of(Factory::Attribute::Callback) + end end describe 'defining a factory with a default strategy parameter' do diff --git a/spec/factory_girl/proxy/stub_spec.rb b/spec/factory_girl/proxy/stub_spec.rb index 4bbbd14..f0dfbcb 100644 --- a/spec/factory_girl/proxy/stub_spec.rb +++ b/spec/factory_girl/proxy/stub_spec.rb @@ -46,11 +46,11 @@ describe Factory::Proxy::Stub do end describe "when asked for the result" do - it "should return the actual instance when asked for the result" do + it "should return the actual instance" do @stub.result.should == @instance end - it "should run the :after_stub callback when asked for the result" do + it "should run the :after_stub callback" do @spy = Object.new stub(@spy).foo @stub.add_callback(:after_stub, proc{ @spy.foo }) diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 3daa288..eedb628 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -37,6 +37,10 @@ describe "integration" do f.after_create {|u| u.last_name = 'Createy' } end + Factory.define :user_with_inherited_callbacks, :parent => :user_with_callbacks do |f| + f.callback(:after_stub) {|u| u.last_name = 'Double-Stubby' } + end + Factory.define :business do |f| f.name 'Supplier of Awesome' f.association :owner, :factory => :user @@ -290,5 +294,11 @@ describe "integration" do @user.first_name.should == 'Buildy' @user.last_name.should == 'Createy' end + + it "should run both the after_stub callback on the factory and the inherited after_stub callback" do + @user = Factory.stub(:user_with_inherited_callbacks) + @user.first_name.should == 'Stubby' + @user.last_name.should == 'Double-Stubby' + end end end