2012-02-08 10:17:57 -05:00
|
|
|
shared_examples_for "strategy without association support" do
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:factory) { double("associate_factory") }
|
2017-10-20 15:20:28 -04:00
|
|
|
let(:attribute) { FactoryBot::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)
|
2017-10-20 15:20:28 -04:00
|
|
|
runner = FactoryBot::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
|
2017-10-20 15:20:28 -04:00
|
|
|
allow(FactoryBot).to receive(:factory_by_name).and_return factory
|
2017-06-01 12:54:02 -04:00
|
|
|
allow(factory).to receive(:compile)
|
|
|
|
allow(factory).to receive(: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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(association_named(:user, {})).to be_nil
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
shared_examples_for "strategy with association support" do |factory_bot_strategy_name|
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:factory) { double("associate_factory") }
|
2012-02-08 12:13:27 -05:00
|
|
|
|
2012-02-10 16:56:07 -05:00
|
|
|
def association_named(name, strategy, overrides)
|
2017-10-20 15:20:28 -04:00
|
|
|
runner = FactoryBot::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
|
2017-10-20 15:20:28 -04:00
|
|
|
allow(FactoryBot).to receive(:factory_by_name).and_return factory
|
2017-06-01 12:54:02 -04:00
|
|
|
allow(factory).to receive(:compile)
|
|
|
|
allow(factory).to receive(: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
|
2017-10-20 15:20:28 -04:00
|
|
|
association_named(:author, factory_bot_strategy_name, great: "value")
|
|
|
|
expect(factory).to have_received(:run).with(factory_bot_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
|
2017-10-20 15:20:28 -04:00
|
|
|
association_named(:author, factory_bot_strategy_name, great: "value")
|
|
|
|
expect(FactoryBot).to have_received(:factory_by_name).with(:author)
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
shared_examples_for "strategy with strategy: :build" do |factory_bot_strategy_name|
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:factory) { double("associate_factory") }
|
2012-02-08 12:13:27 -05:00
|
|
|
|
|
|
|
def association_named(name, overrides)
|
2017-10-20 15:20:28 -04:00
|
|
|
runner = FactoryBot::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
|
2017-10-20 15:20:28 -04:00
|
|
|
allow(FactoryBot).to receive(:factory_by_name).and_return factory
|
2017-06-01 12:54:02 -04:00
|
|
|
allow(factory).to receive(:compile)
|
|
|
|
allow(factory).to receive(: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")
|
2018-10-07 18:02:54 -04:00
|
|
|
expect(factory).to have_received(:run).with(factory_bot_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")
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot).to 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
|
|
|
|
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:evaluation) do
|
|
|
|
double("evaluation", object: result_instance, notify: true, create: nil)
|
|
|
|
end
|
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|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(evaluation).to have_received(:notify).with(name, evaluation.object)
|
2012-04-13 14:20:19 -04:00
|
|
|
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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.result(evaluation)).to eq evaluation.object
|
2011-08-16 23:08:24 -04:00
|
|
|
end
|
|
|
|
end
|