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
|
module FactoryGirl
|
||||||
class AssociationRunner
|
class AssociationRunner
|
||||||
def initialize(factory_name)
|
def initialize(factory_name, strategy_name_or_object, overrides)
|
||||||
@factory_name = factory_name
|
@factory_name = factory_name
|
||||||
|
@strategy_name_or_object = strategy_name_or_object
|
||||||
|
@overrides = overrides
|
||||||
end
|
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
|
strategy = StrategyCalculator.new(strategy_name_or_object).strategy
|
||||||
factory.run(strategy, overrides.except(:method))
|
factory.run(strategy, @overrides)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
require "active_support/core_ext/hash/except"
|
||||||
require "active_support/core_ext/class/attribute"
|
require "active_support/core_ext/class/attribute"
|
||||||
|
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
|
@ -30,8 +31,9 @@ module FactoryGirl
|
||||||
end
|
end
|
||||||
|
|
||||||
def association(factory_name, overrides = {})
|
def association(factory_name, overrides = {})
|
||||||
runner = AssociationRunner.new(factory_name)
|
strategy_name = overrides.delete(:method)
|
||||||
@build_strategy.association(runner, overrides)
|
runner = AssociationRunner.new(factory_name, strategy_name, overrides)
|
||||||
|
@build_strategy.association(runner)
|
||||||
end
|
end
|
||||||
|
|
||||||
def instance=(object_instance)
|
def instance=(object_instance)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Strategy #:nodoc:
|
class Strategy #:nodoc:
|
||||||
class AttributesFor < Strategy #:nodoc:
|
class AttributesFor < Strategy #:nodoc:
|
||||||
def association(runner, overrides)
|
def association(runner)
|
||||||
end
|
end
|
||||||
|
|
||||||
def result(attribute_assigner, to_create)
|
def result(attribute_assigner, to_create)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Strategy #:nodoc:
|
class Strategy #:nodoc:
|
||||||
class Build < Strategy #:nodoc:
|
class Build < Strategy #:nodoc:
|
||||||
def association(runner, overrides)
|
def association(runner)
|
||||||
runner.run(overrides[:method], overrides)
|
runner.run
|
||||||
end
|
end
|
||||||
|
|
||||||
def result(attribute_assigner, to_create)
|
def result(attribute_assigner, to_create)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Strategy #:nodoc:
|
class Strategy #:nodoc:
|
||||||
class Create < Strategy #:nodoc:
|
class Create < Strategy #:nodoc:
|
||||||
def association(runner, overrides)
|
def association(runner)
|
||||||
runner.run(overrides[:method], overrides)
|
runner.run
|
||||||
end
|
end
|
||||||
|
|
||||||
def result(attribute_assigner, to_create)
|
def result(attribute_assigner, to_create)
|
||||||
|
|
|
@ -3,8 +3,8 @@ module FactoryGirl
|
||||||
class Stub < Strategy #:nodoc:
|
class Stub < Strategy #:nodoc:
|
||||||
@@next_id = 1000
|
@@next_id = 1000
|
||||||
|
|
||||||
def association(runner, overrides)
|
def association(runner)
|
||||||
runner.run(Strategy::Stub, overrides)
|
runner.run(Strategy::Stub)
|
||||||
end
|
end
|
||||||
|
|
||||||
def result(attribute_assigner, to_create)
|
def result(attribute_assigner, to_create)
|
||||||
|
|
|
@ -8,24 +8,24 @@ describe FactoryGirl::AssociationRunner do
|
||||||
FactoryGirl.stubs(:factory_by_name => factory)
|
FactoryGirl.stubs(:factory_by_name => factory)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "runs a strategy based on a factory name" do
|
it "passes all overrides to the factory" do
|
||||||
FactoryGirl::AssociationRunner.new(:user).run(FactoryGirl::Strategy::Build, {})
|
FactoryGirl::AssociationRunner.new(:user, FactoryGirl::Strategy::Build, :method => :build, :name => "John").run
|
||||||
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, {})
|
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :method => :build, :name => "John")
|
||||||
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" })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "runs a strategy inferred by name based on a factory name" do
|
it "runs a strategy inferred by name based on a factory name" do
|
||||||
FactoryGirl::AssociationRunner.new(:user).run(:build, { :method => :build, :name => "John" })
|
FactoryGirl::AssociationRunner.new(:user, :build, :method => :build, :name => "John").run
|
||||||
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, { :name => "John" })
|
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
|
end
|
||||||
|
|
||||||
it "raises if the strategy cannot be inferred" do
|
it "raises if the strategy cannot be inferred" do
|
||||||
expect 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.to raise_error("unrecognized method bogus_strategy")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,8 +2,8 @@ shared_examples_for "strategy without association support" do
|
||||||
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
|
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
|
||||||
|
|
||||||
def association_named(name, overrides)
|
def association_named(name, overrides)
|
||||||
runner = FactoryGirl::AssociationRunner.new(name)
|
runner = FactoryGirl::AssociationRunner.new(name, :build, overrides)
|
||||||
subject.association(runner, overrides)
|
subject.association(runner)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil when accessing an association" do
|
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|
|
shared_examples_for "strategy with association support" do |factory_girl_strategy_class|
|
||||||
let(:factory) { stub("associate_factory") }
|
let(:factory) { stub("associate_factory") }
|
||||||
|
|
||||||
def association_named(name, overrides)
|
def association_named(name, strategy, overrides)
|
||||||
runner = FactoryGirl::AssociationRunner.new(name)
|
runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides)
|
||||||
subject.association(runner, overrides)
|
subject.association(runner)
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
@ -31,12 +31,12 @@ shared_examples_for "strategy with association support" do |factory_girl_strateg
|
||||||
end
|
end
|
||||||
|
|
||||||
it "runs the factory with the correct overrides" do
|
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")
|
factory.should have_received(:run).with(factory_girl_strategy_class, :great => "value")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "finds the factory with the correct factory name" do
|
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)
|
FactoryGirl.should have_received(:factory_by_name).with(:author)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -45,8 +45,9 @@ shared_examples_for "strategy with :method => :build" do |factory_girl_strategy_
|
||||||
let(:factory) { stub("associate_factory") }
|
let(:factory) { stub("associate_factory") }
|
||||||
|
|
||||||
def association_named(name, overrides)
|
def association_named(name, overrides)
|
||||||
runner = FactoryGirl::AssociationRunner.new(name)
|
strategy = overrides.delete(:method)
|
||||||
subject.association(runner, overrides)
|
runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides)
|
||||||
|
subject.association(runner)
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
|
Loading…
Reference in a new issue