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

Added 'to_create' to factory definitions, allowing a factory to use something besides #save!

This commit is contained in:
Joe Ferris 2010-11-12 15:21:16 -06:00 committed by Joe Ferris
parent fe836545e0
commit 18c562a1e6
15 changed files with 91 additions and 21 deletions

View file

@ -143,5 +143,9 @@ module FactoryGirl
def after_stub(&block)
@factory.add_callback(:after_stub, &block)
end
def to_create(&block)
@factory.to_create(&block)
end
end
end

View file

@ -98,7 +98,7 @@ module FactoryGirl
@attributes << Attribute::Callback.new(name.to_sym, block)
end
def run (proxy_class, overrides) #:nodoc:
def run(proxy_class, overrides) #:nodoc:
proxy = proxy_class.new(build_class)
overrides = symbolize_keys(overrides)
overrides.each {|attr, val| proxy.set(attr, val) }
@ -108,7 +108,7 @@ module FactoryGirl
attribute.add_to(proxy)
end
end
proxy.result
proxy.result(@to_create_block)
end
def human_name(*args, &block)
@ -148,6 +148,10 @@ module FactoryGirl
@options[:aliases] || []
end
def to_create(&block)
@to_create_block = block
end
private
def class_for (class_or_to_s)

View file

@ -70,7 +70,7 @@ module FactoryGirl
get(method)
end
def result
def result(to_create)
raise NotImplementedError, "Strategies must return a result"
end
end

View file

@ -13,7 +13,7 @@ module FactoryGirl
@hash[attribute] = value
end
def result
def result(to_create)
@hash
end
end

View file

@ -23,7 +23,7 @@ module FactoryGirl
factory.run(Proxy::Create, overrides)
end
def result
def result(to_create)
run_callbacks(:after_build)
@instance
end

View file

@ -1,9 +1,13 @@
module FactoryGirl
class Proxy #:nodoc:
class Create < Build #:nodoc:
def result
def result(to_create)
run_callbacks(:after_build)
@instance.save!
if to_create
to_create.call(@instance)
else
@instance.save!
end
run_callbacks(:after_create)
@instance
end

View file

@ -55,7 +55,7 @@ module FactoryGirl
factory.run(Proxy::Stub, overrides)
end
def result
def result(to_create)
run_callbacks(:after_stub)
@instance
end

View file

@ -31,3 +31,33 @@ describe "a created instance" do
end
end
describe "a custom create" do
before do
define_class('User') do
def initialize
@persisted = false
end
def persist
@persisted = true
end
def persisted?
@persisted
end
end
FactoryGirl.define do
factory :user do
to_create do |user|
user.persist
end
end
end
end
it "uses the custom create block instead of save" do
Factory(:user).should be_persisted
end
end

View file

@ -129,4 +129,11 @@ describe FactoryGirl::DefinitionProxy do
factory.attributes.last.add_to(proxy)
proxy.should have_received.set(name, 'expected')
end
it "delegates to_create" do
result = 'expected'
mock(factory).to_create { result }
subject.to_create.should == result
end
end

View file

@ -97,11 +97,24 @@ describe FactoryGirl::Factory do
end
it "should return the result from the proxy when running" do
mock(@proxy).result() { 'result' }
mock(@proxy).result(nil) { 'result' }
@factory.run(FactoryGirl::Proxy::Build, {}).should == 'result'
end
end
it "passes a custom creation block" do
proxy = 'proxy'
stub(FactoryGirl::Proxy::Build).new { proxy }
stub(proxy).result {}
block = lambda {}
factory = FactoryGirl::Factory.new(:object)
factory.to_create(&block)
factory.run(FactoryGirl::Proxy::Build, {})
proxy.should have_received.result(block)
end
it "should return associations" do
factory = FactoryGirl::Factory.new(:post)
factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))

View file

@ -12,7 +12,7 @@ describe FactoryGirl::Proxy::AttributesFor do
end
it "should not set a value for the association" do
(@proxy.result.key?(:owner)).should_not be
(@proxy.result(nil).key?(:owner)).should_not be
end
end
@ -32,7 +32,7 @@ describe FactoryGirl::Proxy::AttributesFor do
end
it "should return a hash when asked for the result" do
@proxy.result.should be_kind_of(Hash)
@proxy.result(nil).should be_kind_of(Hash)
end
describe "after setting an attribute" do
@ -41,7 +41,7 @@ describe FactoryGirl::Proxy::AttributesFor do
end
it "should set that value in the resulting hash" do
@proxy.result[:attribute].should == 'value'
@proxy.result(nil)[:attribute].should == 'value'
end
it "should return that value when asked for that attribute" do

View file

@ -47,14 +47,14 @@ describe FactoryGirl::Proxy::Build do
end
it "should return the built instance when asked for the result" do
@proxy.result.should == @instance
@proxy.result(nil).should == @instance
end
it "should run the :after_build callback when retrieving the result" do
spy = Object.new
stub(spy).foo
@proxy.add_callback(:after_build, proc{ spy.foo })
@proxy.result
@proxy.result(nil)
spy.should have_received.foo
end

View file

@ -55,7 +55,7 @@ describe FactoryGirl::Proxy::Create do
stub(@create_spy).foo
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
@result = @proxy.result
@result = @proxy.result(nil)
end
it "should save the instance" do
@ -72,6 +72,14 @@ describe FactoryGirl::Proxy::Create do
end
end
it "runs a custom create block" do
block = 'custom create block'
stub(block).call
stub(@instance).save! { raise }
instance = @proxy.result(block)
block.should have_received.call(instance)
end
describe "when setting an attribute" do
before do
@proxy.set(:attribute, 'value')

View file

@ -13,11 +13,11 @@ describe FactoryGirl::Proxy::Stub do
end
it "should not be a new record" do
@stub.result.should_not be_new_record
@stub.result(nil).should_not be_new_record
end
it "should not be able to connect to the database" do
lambda { @stub.result.reload }.should raise_error(RuntimeError)
lambda { @stub.result(nil).reload }.should raise_error(RuntimeError)
end
describe "when a user factory exists" do
@ -37,7 +37,7 @@ describe FactoryGirl::Proxy::Stub do
end
it "should set a value for the association" do
@stub.result.owner.should == @user
@stub.result(nil).owner.should == @user
end
end
@ -48,14 +48,14 @@ describe FactoryGirl::Proxy::Stub do
describe "when asked for the result" do
it "should return the actual instance" do
@stub.result.should == @instance
@stub.result(nil).should == @instance
end
it "should run the :after_stub callback" do
@spy = Object.new
stub(@spy).foo
@stub.add_callback(:after_stub, proc{ @spy.foo })
@stub.result
@stub.result(nil)
@spy.should have_received.foo
end
end

View file

@ -23,7 +23,7 @@ describe FactoryGirl::Proxy do
end
it "should raise an error when asked for the result" do
lambda { @proxy.result }.should raise_error(NotImplementedError)
lambda { @proxy.result(nil) }.should raise_error(NotImplementedError)
end
describe "when adding callbacks" do