From 4dd67d1fb76783e7354b20083dce703ca886e208 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Fri, 10 Feb 2012 16:56:07 -0500 Subject: [PATCH] Move override handling into the evaluator --- lib/factory_girl/association_runner.rb | 13 ++++++------ lib/factory_girl/evaluator.rb | 6 ++++-- lib/factory_girl/strategy/attributes_for.rb | 2 +- lib/factory_girl/strategy/build.rb | 4 ++-- lib/factory_girl/strategy/create.rb | 4 ++-- lib/factory_girl/strategy/stub.rb | 4 ++-- spec/factory_girl/association_runner_spec.rb | 22 ++++++++++---------- spec/support/shared_examples/strategy.rb | 19 +++++++++-------- 8 files changed, 39 insertions(+), 35 deletions(-) diff --git a/lib/factory_girl/association_runner.rb b/lib/factory_girl/association_runner.rb index 03a67c6..d675c57 100644 --- a/lib/factory_girl/association_runner.rb +++ b/lib/factory_girl/association_runner.rb @@ -1,14 +1,15 @@ -require "active_support/core_ext/hash/except" - module FactoryGirl class AssociationRunner - def initialize(factory_name) - @factory_name = factory_name + def initialize(factory_name, strategy_name_or_object, overrides) + @factory_name = factory_name + @strategy_name_or_object = strategy_name_or_object + @overrides = overrides end - def run(strategy_name_or_object, overrides) + def run(strategy_override = nil) + strategy_name_or_object = strategy_override || @strategy_name_or_object strategy = StrategyCalculator.new(strategy_name_or_object).strategy - factory.run(strategy, overrides.except(:method)) + factory.run(strategy, @overrides) end private diff --git a/lib/factory_girl/evaluator.rb b/lib/factory_girl/evaluator.rb index 1dbd83d..1b7755f 100644 --- a/lib/factory_girl/evaluator.rb +++ b/lib/factory_girl/evaluator.rb @@ -1,3 +1,4 @@ +require "active_support/core_ext/hash/except" require "active_support/core_ext/class/attribute" module FactoryGirl @@ -30,8 +31,9 @@ module FactoryGirl end def association(factory_name, overrides = {}) - runner = AssociationRunner.new(factory_name) - @build_strategy.association(runner, overrides) + strategy_name = overrides.delete(:method) + runner = AssociationRunner.new(factory_name, strategy_name, overrides) + @build_strategy.association(runner) end def instance=(object_instance) diff --git a/lib/factory_girl/strategy/attributes_for.rb b/lib/factory_girl/strategy/attributes_for.rb index 2611695..6fb51fc 100644 --- a/lib/factory_girl/strategy/attributes_for.rb +++ b/lib/factory_girl/strategy/attributes_for.rb @@ -1,7 +1,7 @@ module FactoryGirl class Strategy #:nodoc: class AttributesFor < Strategy #:nodoc: - def association(runner, overrides) + def association(runner) end def result(attribute_assigner, to_create) diff --git a/lib/factory_girl/strategy/build.rb b/lib/factory_girl/strategy/build.rb index 54e5b49..4832ac3 100644 --- a/lib/factory_girl/strategy/build.rb +++ b/lib/factory_girl/strategy/build.rb @@ -1,8 +1,8 @@ module FactoryGirl class Strategy #:nodoc: class Build < Strategy #:nodoc: - def association(runner, overrides) - runner.run(overrides[:method], overrides) + def association(runner) + runner.run end def result(attribute_assigner, to_create) diff --git a/lib/factory_girl/strategy/create.rb b/lib/factory_girl/strategy/create.rb index 0d8d3f5..4cf2be1 100644 --- a/lib/factory_girl/strategy/create.rb +++ b/lib/factory_girl/strategy/create.rb @@ -1,8 +1,8 @@ module FactoryGirl class Strategy #:nodoc: class Create < Strategy #:nodoc: - def association(runner, overrides) - runner.run(overrides[:method], overrides) + def association(runner) + runner.run end def result(attribute_assigner, to_create) diff --git a/lib/factory_girl/strategy/stub.rb b/lib/factory_girl/strategy/stub.rb index b51ae92..ca8740a 100644 --- a/lib/factory_girl/strategy/stub.rb +++ b/lib/factory_girl/strategy/stub.rb @@ -3,8 +3,8 @@ module FactoryGirl class Stub < Strategy #:nodoc: @@next_id = 1000 - def association(runner, overrides) - runner.run(Strategy::Stub, overrides) + def association(runner) + runner.run(Strategy::Stub) end def result(attribute_assigner, to_create) diff --git a/spec/factory_girl/association_runner_spec.rb b/spec/factory_girl/association_runner_spec.rb index 23bc7df..713711b 100644 --- a/spec/factory_girl/association_runner_spec.rb +++ b/spec/factory_girl/association_runner_spec.rb @@ -8,24 +8,24 @@ describe FactoryGirl::AssociationRunner do FactoryGirl.stubs(:factory_by_name => factory) end - it "runs a strategy based on a factory name" do - FactoryGirl::AssociationRunner.new(:user).run(FactoryGirl::Strategy::Build, {}) - factory.should have_received(:run).with(FactoryGirl::Strategy::Build, {}) - end - - it "strips only method from overrides" do - FactoryGirl::AssociationRunner.new(:user).run(FactoryGirl::Strategy::Build, { :method => :build, :name => "John" }) - factory.should have_received(:run).with(FactoryGirl::Strategy::Build, { :name => "John" }) + it "passes all overrides to the factory" do + FactoryGirl::AssociationRunner.new(:user, FactoryGirl::Strategy::Build, :method => :build, :name => "John").run + factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :method => :build, :name => "John") end it "runs a strategy inferred by name based on a factory name" do - FactoryGirl::AssociationRunner.new(:user).run(:build, { :method => :build, :name => "John" }) - factory.should have_received(:run).with(FactoryGirl::Strategy::Build, { :name => "John" }) + FactoryGirl::AssociationRunner.new(:user, :build, :method => :build, :name => "John").run + factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :method => :build, :name => "John") + end + + it "allows overriding strategy" do + FactoryGirl::AssociationRunner.new(:user, :build, :method => :build, :name => "John").run(FactoryGirl::Strategy::Create) + factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :method => :build, :name => "John") end it "raises if the strategy cannot be inferred" do expect do - FactoryGirl::AssociationRunner.new(:user).run(:bogus_strategy, { :method => :build, :name => "John" }) + FactoryGirl::AssociationRunner.new(:user, :bogus_strategy, :method => :build, :name => "John").run end.to raise_error("unrecognized method bogus_strategy") end end diff --git a/spec/support/shared_examples/strategy.rb b/spec/support/shared_examples/strategy.rb index 3d6a478..18102c8 100644 --- a/spec/support/shared_examples/strategy.rb +++ b/spec/support/shared_examples/strategy.rb @@ -2,8 +2,8 @@ shared_examples_for "strategy without association support" do let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) } def association_named(name, overrides) - runner = FactoryGirl::AssociationRunner.new(name) - subject.association(runner, overrides) + runner = FactoryGirl::AssociationRunner.new(name, :build, overrides) + subject.association(runner) end it "returns nil when accessing an association" do @@ -20,9 +20,9 @@ end shared_examples_for "strategy with association support" do |factory_girl_strategy_class| let(:factory) { stub("associate_factory") } - def association_named(name, overrides) - runner = FactoryGirl::AssociationRunner.new(name) - subject.association(runner, overrides) + def association_named(name, strategy, overrides) + runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides) + subject.association(runner) end before do @@ -31,12 +31,12 @@ shared_examples_for "strategy with association support" do |factory_girl_strateg end it "runs the factory with the correct overrides" do - association_named(:author, :great => "value") + association_named(:author, factory_girl_strategy_class, :great => "value") factory.should have_received(:run).with(factory_girl_strategy_class, :great => "value") end it "finds the factory with the correct factory name" do - association_named(:author, :great => "value") + association_named(:author, factory_girl_strategy_class, :great => "value") FactoryGirl.should have_received(:factory_by_name).with(:author) end end @@ -45,8 +45,9 @@ shared_examples_for "strategy with :method => :build" do |factory_girl_strategy_ let(:factory) { stub("associate_factory") } def association_named(name, overrides) - runner = FactoryGirl::AssociationRunner.new(name) - subject.association(runner, overrides) + strategy = overrides.delete(:method) + runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides) + subject.association(runner) end before do