2012-02-08 10:17:57 -05:00
|
|
|
shared_examples_for "strategy without association support" do
|
2011-11-01 17:00:26 -04:00
|
|
|
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
|
2011-08-16 23:08:24 -04:00
|
|
|
|
2012-02-08 12:13:27 -05:00
|
|
|
def association_named(name, overrides)
|
|
|
|
runner = FactoryGirl::AssociationRunner.new(name)
|
|
|
|
subject.association(runner, overrides)
|
|
|
|
end
|
|
|
|
|
2011-11-01 17:00:26 -04:00
|
|
|
it "returns nil when accessing an association" do
|
2012-02-08 12:13:27 -05:00
|
|
|
association_named(:user, {}).should be_nil
|
2011-11-01 17:00:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not attempt to look up the factory when accessing the association" do
|
|
|
|
FactoryGirl.stubs(:factory_by_name)
|
2012-02-08 12:13:27 -05:00
|
|
|
association_named(:awesome, {})
|
2011-11-01 17:00:26 -04:00
|
|
|
FactoryGirl.should have_received(:factory_by_name).never
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
shared_examples_for "strategy with association support" do |factory_girl_strategy_class|
|
2012-02-08 12:13:27 -05:00
|
|
|
let(:factory) { stub("associate_factory") }
|
|
|
|
|
|
|
|
def association_named(name, overrides)
|
|
|
|
runner = FactoryGirl::AssociationRunner.new(name)
|
|
|
|
subject.association(runner, overrides)
|
|
|
|
end
|
2011-08-16 23:08:24 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
FactoryGirl.stubs(:factory_by_name => factory)
|
2011-11-01 17:00:26 -04:00
|
|
|
factory.stubs(:run)
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2011-11-01 17:00:26 -04:00
|
|
|
it "runs the factory with the correct overrides" do
|
2012-02-08 12:13:27 -05:00
|
|
|
association_named(:author, :great => "value")
|
|
|
|
factory.should have_received(:run).with(factory_girl_strategy_class, :great => "value")
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2011-11-01 17:00:26 -04:00
|
|
|
it "finds the factory with the correct factory name" do
|
2012-02-08 12:13:27 -05:00
|
|
|
association_named(:author, :great => "value")
|
|
|
|
FactoryGirl.should have_received(:factory_by_name).with(:author)
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
shared_examples_for "strategy with :method => :build" do |factory_girl_strategy_class|
|
2012-02-08 12:13:27 -05:00
|
|
|
let(:factory) { stub("associate_factory") }
|
|
|
|
|
|
|
|
def association_named(name, overrides)
|
|
|
|
runner = FactoryGirl::AssociationRunner.new(name)
|
|
|
|
subject.association(runner, overrides)
|
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
FactoryGirl.stubs(:factory_by_name => factory)
|
2011-11-01 17:00:26 -04:00
|
|
|
factory.stubs(:run)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
|
2011-11-01 17:00:26 -04:00
|
|
|
it "runs the factory with the correct overrides" do
|
2012-02-08 12:13:27 -05:00
|
|
|
association_named(:author, :method => :build, :great => "value")
|
2012-02-08 10:17:57 -05:00
|
|
|
factory.should have_received(:run).with(factory_girl_strategy_class, { :great => "value" })
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
|
2011-11-01 17:00:26 -04:00
|
|
|
it "finds the factory with the correct factory name" do
|
2012-02-08 12:13:27 -05:00
|
|
|
association_named(:author, :method => :build, :great => "value")
|
|
|
|
FactoryGirl.should have_received(:factory_by_name).with(:author)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
shared_examples_for "strategy with callbacks" do |*callback_names|
|
2011-12-16 14:10:25 -05:00
|
|
|
let(:callback_observer) do
|
|
|
|
define_class("CallbackObserver") do
|
|
|
|
attr_reader :callbacks_called
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@callbacks_called = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(callback_name, assigner)
|
|
|
|
@callbacks_called << [callback_name, assigner]
|
|
|
|
end
|
|
|
|
end.new
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2011-12-16 14:10:25 -05:00
|
|
|
let(:result_instance) do
|
|
|
|
define_class("ResultInstance") do
|
|
|
|
attr_accessor :id
|
|
|
|
end.new
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2011-12-16 14:10:25 -05:00
|
|
|
let(:assigner) { stub("attribute assigner", :object => result_instance) }
|
2011-08-16 23:08:24 -04:00
|
|
|
|
2011-12-16 14:10:25 -05:00
|
|
|
before { subject.add_observer(callback_observer) }
|
2011-08-16 23:08:24 -04:00
|
|
|
|
2011-12-16 14:10:25 -05:00
|
|
|
it "runs the callbacks #{callback_names} with the assigner's object" do
|
|
|
|
subject.result(assigner, lambda {|instance| instance })
|
|
|
|
callback_observer.callbacks_called.should == callback_names.map {|name| [name, assigner.object] }
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2011-12-16 14:10:25 -05:00
|
|
|
it "returns the object from the assigner" do
|
|
|
|
subject.result(assigner, lambda {|instance| instance }).should == assigner.object
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|