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:
parent
fe836545e0
commit
18c562a1e6
15 changed files with 91 additions and 21 deletions
|
@ -143,5 +143,9 @@ module FactoryGirl
|
||||||
def after_stub(&block)
|
def after_stub(&block)
|
||||||
@factory.add_callback(:after_stub, &block)
|
@factory.add_callback(:after_stub, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_create(&block)
|
||||||
|
@factory.to_create(&block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -98,7 +98,7 @@ module FactoryGirl
|
||||||
@attributes << Attribute::Callback.new(name.to_sym, block)
|
@attributes << Attribute::Callback.new(name.to_sym, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run (proxy_class, overrides) #:nodoc:
|
def run(proxy_class, overrides) #:nodoc:
|
||||||
proxy = proxy_class.new(build_class)
|
proxy = proxy_class.new(build_class)
|
||||||
overrides = symbolize_keys(overrides)
|
overrides = symbolize_keys(overrides)
|
||||||
overrides.each {|attr, val| proxy.set(attr, val) }
|
overrides.each {|attr, val| proxy.set(attr, val) }
|
||||||
|
@ -108,7 +108,7 @@ module FactoryGirl
|
||||||
attribute.add_to(proxy)
|
attribute.add_to(proxy)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
proxy.result
|
proxy.result(@to_create_block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def human_name(*args, &block)
|
def human_name(*args, &block)
|
||||||
|
@ -148,6 +148,10 @@ module FactoryGirl
|
||||||
@options[:aliases] || []
|
@options[:aliases] || []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_create(&block)
|
||||||
|
@to_create_block = block
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def class_for (class_or_to_s)
|
def class_for (class_or_to_s)
|
||||||
|
|
|
@ -70,7 +70,7 @@ module FactoryGirl
|
||||||
get(method)
|
get(method)
|
||||||
end
|
end
|
||||||
|
|
||||||
def result
|
def result(to_create)
|
||||||
raise NotImplementedError, "Strategies must return a result"
|
raise NotImplementedError, "Strategies must return a result"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ module FactoryGirl
|
||||||
@hash[attribute] = value
|
@hash[attribute] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
def result
|
def result(to_create)
|
||||||
@hash
|
@hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ module FactoryGirl
|
||||||
factory.run(Proxy::Create, overrides)
|
factory.run(Proxy::Create, overrides)
|
||||||
end
|
end
|
||||||
|
|
||||||
def result
|
def result(to_create)
|
||||||
run_callbacks(:after_build)
|
run_callbacks(:after_build)
|
||||||
@instance
|
@instance
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Proxy #:nodoc:
|
class Proxy #:nodoc:
|
||||||
class Create < Build #:nodoc:
|
class Create < Build #:nodoc:
|
||||||
def result
|
def result(to_create)
|
||||||
run_callbacks(:after_build)
|
run_callbacks(:after_build)
|
||||||
@instance.save!
|
if to_create
|
||||||
|
to_create.call(@instance)
|
||||||
|
else
|
||||||
|
@instance.save!
|
||||||
|
end
|
||||||
run_callbacks(:after_create)
|
run_callbacks(:after_create)
|
||||||
@instance
|
@instance
|
||||||
end
|
end
|
||||||
|
|
|
@ -55,7 +55,7 @@ module FactoryGirl
|
||||||
factory.run(Proxy::Stub, overrides)
|
factory.run(Proxy::Stub, overrides)
|
||||||
end
|
end
|
||||||
|
|
||||||
def result
|
def result(to_create)
|
||||||
run_callbacks(:after_stub)
|
run_callbacks(:after_stub)
|
||||||
@instance
|
@instance
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,3 +31,33 @@ describe "a created instance" do
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
||||||
|
|
|
@ -129,4 +129,11 @@ describe FactoryGirl::DefinitionProxy do
|
||||||
factory.attributes.last.add_to(proxy)
|
factory.attributes.last.add_to(proxy)
|
||||||
proxy.should have_received.set(name, 'expected')
|
proxy.should have_received.set(name, 'expected')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "delegates to_create" do
|
||||||
|
result = 'expected'
|
||||||
|
mock(factory).to_create { result }
|
||||||
|
|
||||||
|
subject.to_create.should == result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -97,11 +97,24 @@ describe FactoryGirl::Factory do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return the result from the proxy when running" do
|
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'
|
@factory.run(FactoryGirl::Proxy::Build, {}).should == 'result'
|
||||||
end
|
end
|
||||||
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
|
it "should return associations" do
|
||||||
factory = FactoryGirl::Factory.new(:post)
|
factory = FactoryGirl::Factory.new(:post)
|
||||||
factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))
|
factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))
|
||||||
|
|
|
@ -12,7 +12,7 @@ describe FactoryGirl::Proxy::AttributesFor do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not set a value for the association" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ describe FactoryGirl::Proxy::AttributesFor do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a hash when asked for the result" do
|
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
|
end
|
||||||
|
|
||||||
describe "after setting an attribute" do
|
describe "after setting an attribute" do
|
||||||
|
@ -41,7 +41,7 @@ describe FactoryGirl::Proxy::AttributesFor do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should set that value in the resulting hash" do
|
it "should set that value in the resulting hash" do
|
||||||
@proxy.result[:attribute].should == 'value'
|
@proxy.result(nil)[:attribute].should == 'value'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return that value when asked for that attribute" do
|
it "should return that value when asked for that attribute" do
|
||||||
|
|
|
@ -47,14 +47,14 @@ describe FactoryGirl::Proxy::Build do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return the built instance when asked for the result" do
|
it "should return the built instance when asked for the result" do
|
||||||
@proxy.result.should == @instance
|
@proxy.result(nil).should == @instance
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should run the :after_build callback when retrieving the result" do
|
it "should run the :after_build callback when retrieving the result" do
|
||||||
spy = Object.new
|
spy = Object.new
|
||||||
stub(spy).foo
|
stub(spy).foo
|
||||||
@proxy.add_callback(:after_build, proc{ spy.foo })
|
@proxy.add_callback(:after_build, proc{ spy.foo })
|
||||||
@proxy.result
|
@proxy.result(nil)
|
||||||
spy.should have_received.foo
|
spy.should have_received.foo
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ describe FactoryGirl::Proxy::Create do
|
||||||
stub(@create_spy).foo
|
stub(@create_spy).foo
|
||||||
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
|
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
|
||||||
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
|
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
|
||||||
@result = @proxy.result
|
@result = @proxy.result(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should save the instance" do
|
it "should save the instance" do
|
||||||
|
@ -72,6 +72,14 @@ describe FactoryGirl::Proxy::Create do
|
||||||
end
|
end
|
||||||
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
|
describe "when setting an attribute" do
|
||||||
before do
|
before do
|
||||||
@proxy.set(:attribute, 'value')
|
@proxy.set(:attribute, 'value')
|
||||||
|
|
|
@ -13,11 +13,11 @@ describe FactoryGirl::Proxy::Stub do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not be a new record" do
|
it "should not be a new record" do
|
||||||
@stub.result.should_not be_new_record
|
@stub.result(nil).should_not be_new_record
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not be able to connect to the database" do
|
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
|
end
|
||||||
|
|
||||||
describe "when a user factory exists" do
|
describe "when a user factory exists" do
|
||||||
|
@ -37,7 +37,7 @@ describe FactoryGirl::Proxy::Stub do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should set a value for the association" do
|
it "should set a value for the association" do
|
||||||
@stub.result.owner.should == @user
|
@stub.result(nil).owner.should == @user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -48,14 +48,14 @@ describe FactoryGirl::Proxy::Stub do
|
||||||
|
|
||||||
describe "when asked for the result" do
|
describe "when asked for the result" do
|
||||||
it "should return the actual instance" do
|
it "should return the actual instance" do
|
||||||
@stub.result.should == @instance
|
@stub.result(nil).should == @instance
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should run the :after_stub callback" do
|
it "should run the :after_stub callback" do
|
||||||
@spy = Object.new
|
@spy = Object.new
|
||||||
stub(@spy).foo
|
stub(@spy).foo
|
||||||
@stub.add_callback(:after_stub, proc{ @spy.foo })
|
@stub.add_callback(:after_stub, proc{ @spy.foo })
|
||||||
@stub.result
|
@stub.result(nil)
|
||||||
@spy.should have_received.foo
|
@spy.should have_received.foo
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ describe FactoryGirl::Proxy do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an error when asked for the result" do
|
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
|
end
|
||||||
|
|
||||||
describe "when adding callbacks" do
|
describe "when adding callbacks" do
|
||||||
|
|
Loading…
Add table
Reference in a new issue