mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Move override handling into the evaluator
This commit is contained in:
parent
1dc5d701d5
commit
4dd67d1fb7
8 changed files with 39 additions and 35 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue