Remove default_strategy

This commit is contained in:
Joshua Clayton 2012-03-09 15:24:40 -05:00
parent b7f3789072
commit 7623531324
9 changed files with 10 additions and 144 deletions

View File

@ -11,7 +11,6 @@ module FactoryGirl
@parent = options[:parent]
@aliases = options[:aliases] || []
@class_name = options[:class]
@default_strategy = options[:default_strategy]
@definition = Definition.new(@name, options[:traits] || [])
@compiled = false
end
@ -32,10 +31,6 @@ module FactoryGirl
end
end
def default_strategy #:nodoc:
@default_strategy || parent.default_strategy
end
def run(strategy_class, overrides, &block) #:nodoc:
block ||= lambda {|result| result }
compile
@ -130,13 +125,7 @@ module FactoryGirl
private
def assert_valid_options(options)
options.assert_valid_keys(:class, :parent, :default_strategy, :aliases, :traits)
if options[:default_strategy]
Strategy.ensure_strategy_exists!(options[:default_strategy])
$stderr.puts "DEPRECATION WARNING: default_strategy is deprecated."
$stderr.puts "Override to_create if you need to prevent a call to #save!."
end
options.assert_valid_keys(:class, :parent, :aliases, :traits)
end
def parent

View File

@ -10,7 +10,6 @@ module FactoryGirl
def compile; end
def class_name; end
def default_strategy; :create; end
def evaluator_class; FactoryGirl::Evaluator; end
end
end

View File

@ -17,10 +17,6 @@ module FactoryGirl
@value = @value.next
end
def default_strategy
:create
end
def names
[@name]
end

View File

@ -17,10 +17,6 @@ module FactoryGirl
# The parent factory. If specified, the attributes from the parent
# factory will be copied to the current one with an ability to override
# them.
# * default_strategy: +Symbol+
# DEPRECATED.
# The strategy that will be used by the Factory shortcut method.
# Defaults to :create.
#
# Yields: +Factory+
# The newly created factory.
@ -31,25 +27,6 @@ module FactoryGirl
FactoryGirl.register_factory(factory)
end
# Executes the default strategy for the given factory. This is usually create,
# but it can be overridden for each factory.
#
# DEPRECATED
#
# Use create instead.
#
# Arguments:
# * name: +Symbol+ or +String+
# The name of the factory that should be used.
# * overrides: +Hash+
# Attributes to overwrite for this instance.
#
# Returns: +Object+
# The result of the default strategy.
def self.default_strategy(name, overrides = {})
FactoryGirl.send(FactoryGirl.factory_by_name(name).default_strategy, name, overrides)
end
# Defines a new sequence that can be used to generate unique values in a specific format.
#
# Arguments:
@ -130,17 +107,12 @@ module FactoryGirl
end
end
# Shortcut for Factory.default_strategy.
#
# DEPRECATION WARNING:
#
# In a future release, default_strategy will be removed and this will
# simply call create instead.
# Shortcut for Factory.create.
#
# Example:
# Factory(:user, :name => 'Joe')
def Factory(name, attrs = {})
::Factory.default_strategy(name, attrs)
::Factory.create(name, attrs)
end
end
end

View File

@ -1,24 +0,0 @@
require 'spec_helper'
describe "default strategy" do
it "uses create when not specified" do
define_model('User')
FactoryGirl.define do
factory :user
end
Factory(:user).should_not be_new_record
end
it "can be overridden" do
define_model('User')
FactoryGirl.define do
factory :user, :default_strategy => :build
end
Factory(:user).should be_new_record
end
end

View File

@ -29,12 +29,6 @@ describe "vintage syntax" do
end
end
it "raises an ArgumentError when trying to use a non-existent strategy" do
expect {
Factory.define(:object, :default_strategy => :nonexistent) {}
}.to raise_error(ArgumentError)
end
it "raises Factory::SequenceAbuseError" do
Factory.define :sequence_abuser, :class => User do |factory|
factory.first_name { Factory.sequence(:name) }
@ -117,18 +111,6 @@ describe "after defining a factory" do
@factory.should have_received(:run).with(FactoryGirl::Strategy::Stub, :attr => 'value')
end
it "uses default strategy option as Factory.default_strategy" do
@factory.stubs(:default_strategy => :create, :run => "result")
Factory.default_strategy(@name, :attr => 'value').should == 'result'
@factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
end
it "uses the default strategy for the global Factory method" do
@factory.stubs(:default_strategy => :create, :run => "result")
Factory(@name, :attr => 'value').should == 'result'
@factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
end
[:build, :create, :attributes_for, :stub].each do |method|
it "raises an ArgumentError on #{method} with a nonexistent factory" do
expect { Factory.send(method, :bogus) }.to raise_error(ArgumentError)

View File

@ -20,10 +20,6 @@ describe FactoryGirl::Factory do
@factory.build_class.should == @class
end
it "has a default strategy" do
@factory.default_strategy.should == :create
end
it "passes a custom creation block" do
strategy = stub("strategy", :result => nil, :add_observer => true)
FactoryGirl::Strategy::Build.stubs(:new => strategy)
@ -207,48 +203,6 @@ describe FactoryGirl::Factory, "for namespaced class" do
end
end
describe FactoryGirl::Factory do
let(:factory_with_non_existent_strategy) do
FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) { }
end
let(:factory_with_stub_strategy) do
FactoryGirl::Factory.new(:object, :default_strategy => :stub)
end
before do
define_class("User")
define_class("Admin", User)
FactoryGirl.register_factory(factory_with_stub_strategy)
end
it "raises when trying to use a non-existent strategy" do
expect { factory_with_non_existent_strategy }.to raise_error
end
it "creates a new factory with a specified default strategy" do
factory_with_stub_strategy.default_strategy.should == :stub
end
describe "defining a child factory without setting default strategy" do
subject { FactoryGirl::Factory.new(:other_object, :parent => factory_with_stub_strategy.name) }
before { subject.compile }
it "inherits default strategy from its parent" do
subject.default_strategy.should == :stub
end
end
describe "defining a child factory with a default strategy" do
subject { FactoryGirl::Factory.new(:other_object, :default_strategy => :build, :parent => factory_with_stub_strategy.name) }
before { subject.compile }
it "overrides the default strategy from parent" do
subject.default_strategy.should == :build
end
end
end
describe FactoryGirl::Factory, "human names" do
context "factory name without underscores" do
subject { FactoryGirl::Factory.new(:user) }

View File

@ -6,9 +6,8 @@ describe FactoryGirl::NullFactory do
it { should delegate(:attributes).to(:definition) }
it { should delegate(:constructor).to(:definition) }
its(:compile) { should be_nil }
its(:class_name) { should be_nil }
its(:default_strategy) { should == :create }
its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) }
its(:evaluator_class) { should == FactoryGirl::Evaluator }
its(:compile) { should be_nil }
its(:class_name) { should be_nil }
its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) }
its(:evaluator_class) { should == FactoryGirl::Evaluator }
end

View File

@ -5,10 +5,9 @@ describe FactoryGirl::Sequence do
let(:name) { :test }
subject { FactoryGirl::Sequence.new(name) {|n| "=#{n}" } }
its(:name) { should == name }
its(:names) { should == [name] }
its(:next) { should == "=1" }
its(:default_strategy) { should == :create }
its(:name) { should == name }
its(:names) { should == [name] }
its(:next) { should == "=1" }
describe "when incrementing" do
before { subject.next }