2012-02-08 10:17:57 -05:00
|
|
|
shared_examples_for "strategy without association support" do
|
2012-03-30 17:30:20 -04:00
|
|
|
let(:factory) { stub("associate_factory") }
|
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)
|
2012-05-04 13:36:46 -04:00
|
|
|
runner = FactoryGirl::FactoryRunner.new(name, :build, [overrides])
|
2012-02-10 16:56:07 -05:00
|
|
|
subject.association(runner)
|
2012-02-08 12:13:27 -05:00
|
|
|
end
|
|
|
|
|
2012-03-30 17:30:20 -04:00
|
|
|
before do
|
|
|
|
FactoryGirl.stubs(factory_by_name: factory)
|
|
|
|
factory.stubs(:compile)
|
|
|
|
factory.stubs(:run)
|
2011-11-01 17:00:26 -04:00
|
|
|
end
|
|
|
|
|
2012-03-30 17:30:20 -04:00
|
|
|
it "returns nil when accessing an association" do
|
|
|
|
association_named(:user, {}).should be_nil
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-04 13:36:46 -04:00
|
|
|
shared_examples_for "strategy with association support" do |factory_girl_strategy_name|
|
2012-02-08 12:13:27 -05:00
|
|
|
let(:factory) { stub("associate_factory") }
|
|
|
|
|
2012-02-10 16:56:07 -05:00
|
|
|
def association_named(name, strategy, overrides)
|
2012-02-17 23:19:57 -05:00
|
|
|
runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides])
|
2012-02-10 16:56:07 -05:00
|
|
|
subject.association(runner)
|
2012-02-08 12:13:27 -05:00
|
|
|
end
|
2011-08-16 23:08:24 -04:00
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
FactoryGirl.stubs(factory_by_name: factory)
|
2012-03-09 14:05:05 -05:00
|
|
|
factory.stubs(:compile)
|
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-05-04 13:36:46 -04:00
|
|
|
association_named(:author, factory_girl_strategy_name, great: "value")
|
|
|
|
factory.should have_received(:run).with(factory_girl_strategy_name, 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-05-04 13:36:46 -04:00
|
|
|
association_named(:author, factory_girl_strategy_name, great: "value")
|
2012-02-08 12:13:27 -05:00
|
|
|
FactoryGirl.should have_received(:factory_by_name).with(:author)
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-04 13:36:46 -04:00
|
|
|
shared_examples_for "strategy with strategy: :build" do |factory_girl_strategy_name|
|
2012-02-08 12:13:27 -05:00
|
|
|
let(:factory) { stub("associate_factory") }
|
|
|
|
|
|
|
|
def association_named(name, overrides)
|
2012-05-04 13:36:46 -04:00
|
|
|
runner = FactoryGirl::FactoryRunner.new(name, overrides[:strategy], [overrides.except(:strategy)])
|
2012-02-10 16:56:07 -05:00
|
|
|
subject.association(runner)
|
2012-02-08 12:13:27 -05:00
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
FactoryGirl.stubs(factory_by_name: factory)
|
2012-03-09 14:05:05 -05:00
|
|
|
factory.stubs(:compile)
|
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-03-09 17:20:38 -05:00
|
|
|
association_named(:author, strategy: :build, great: "value")
|
2012-05-04 13:36:46 -04:00
|
|
|
factory.should have_received(:run).with(factory_girl_strategy_name, { 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-03-09 17:20:38 -05:00
|
|
|
association_named(:author, strategy: :build, great: "value")
|
2012-02-17 11:29:11 -05:00
|
|
|
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(:result_instance) do
|
|
|
|
define_class("ResultInstance") do
|
|
|
|
attr_accessor :id
|
|
|
|
end.new
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2012-04-13 14:20:19 -04:00
|
|
|
let(:evaluation) { stub("evaluation", object: result_instance, notify: true, create: nil) }
|
2011-08-16 23:08:24 -04:00
|
|
|
|
2012-04-13 14:20:19 -04:00
|
|
|
it "runs the callbacks #{callback_names} with the evaluation's object" do
|
|
|
|
subject.result(evaluation)
|
|
|
|
callback_names.each do |name|
|
|
|
|
evaluation.should have_received(:notify).with(name, evaluation.object)
|
|
|
|
end
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
|
2012-04-13 14:20:19 -04:00
|
|
|
it "returns the object from the evaluation" do
|
|
|
|
subject.result(evaluation).should == evaluation.object
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|