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

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.
This commit is contained in:
Nathan Sutton 2009-10-10 00:23:57 -05:00
parent 6e35bf9207
commit 8f3b24a559
4 changed files with 27 additions and 3 deletions

View file

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

View file

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

View file

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

View file

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