Skip to content

Commit

Permalink
Fixing inheritance of callbacks
Browse files Browse the repository at this point in the history
Since callbacks share the same names they wouldn't be inherited.
I added a check to allow them to be inherited.
  • Loading branch information
Nathan Sutton committed Oct 10, 2009
1 parent 6e35bf9 commit 8f3b24a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/factory_girl/factory.rb
Expand Up @@ -351,7 +351,7 @@ def factory_name_for (class_or_to_s)
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)
Expand Down
14 changes: 14 additions & 0 deletions spec/factory_girl/factory_spec.rb
Expand Up @@ -458,6 +458,20 @@ class Other; end
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
Expand Down
4 changes: 2 additions & 2 deletions spec/factory_girl/proxy/stub_spec.rb
Expand Up @@ -46,11 +46,11 @@
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 })
Expand Down
10 changes: 10 additions & 0 deletions spec/integration_spec.rb
Expand Up @@ -37,6 +37,10 @@
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
Expand Down Expand Up @@ -290,5 +294,11 @@
@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

0 comments on commit 8f3b24a

Please sign in to comment.