Deprecate :method in favor of :strategy

This commit is contained in:
Joshua Clayton 2012-02-17 11:29:11 -05:00
parent d0a253fe85
commit 791591bd58
9 changed files with 117 additions and 44 deletions

View File

@ -246,12 +246,12 @@ post.new_record? # => true
post.author.new_record? # => false
```
To not save the associated object, specify :method => :build in the factory:
To not save the associated object, specify :strategy => :build in the factory:
```ruby
factory :post do
# ...
association :author, :factory => :user, :method => :build
association :author, :factory => :user, :strategy => :build
end
# Builds a User, and then builds a Post, but does not save either

View File

@ -31,7 +31,14 @@ module FactoryGirl
end
def association(factory_name, overrides = {})
runner = AssociationRunner.new(factory_name, overrides[:method], overrides.except(:method))
build_strategy = if overrides.has_key?(:method)
$stderr.puts "DEPRECATION WARNING: using :method to specify a build strategy is deprecated; use :strategy instead"
overrides[:method]
elsif overrides.has_key?(:strategy)
overrides[:strategy]
end
runner = AssociationRunner.new(factory_name, build_strategy, overrides.except(:method, :strategy))
@build_strategy.association(runner)
end

View File

@ -31,7 +31,7 @@ describe "a built instance" do
end
end
describe "a built instance with :method => :build" do
describe "a built instance with :strategy => :build" do
include FactoryGirl::Syntax::Methods
before do
@ -45,7 +45,7 @@ describe "a built instance with :method => :build" do
factory :user
factory :post do
association(:user, :method => :build)
association(:user, :strategy => :build)
end
end
end

View File

@ -31,16 +31,10 @@ describe "a created instance" do
end
end
describe "a created instance, specifying :method => build" do
describe "a created instance, specifying :strategy => build" do
include FactoryGirl::Syntax::Methods
before do
define_model('User')
define_model('Post', :user_id => :integer) do
belongs_to :user
end
def define_factories_with_method
FactoryGirl.define do
factory :user
@ -50,11 +44,46 @@ describe "a created instance, specifying :method => build" do
end
end
subject { create('post') }
def define_factories_with_strategy
FactoryGirl.define do
factory :user
it "still saves associations (:method => :build only affects build, not create)" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
factory :post do
association(:user, :strategy => :build)
end
end
end
before do
define_model('User')
define_model('Post', :user_id => :integer) do
belongs_to :user
end
end
context "associations declared with :strategy" do
before { define_factories_with_strategy }
subject { build_stubbed(:post) }
subject { create('post') }
it "still saves associations (:strategy => :build only affects build, not create)" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end
context "associations declared with :method" do
before { define_factories_with_method }
subject { build_stubbed(:post) }
subject { create('post') }
it "still saves associations (:method => :build only affects build, not create)" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end
end

View File

@ -31,16 +31,10 @@ describe "a stubbed instance" do
end
end
describe "a stubbed instance with :method => :build" do
describe "a stubbed instance overriding strategy" do
include FactoryGirl::Syntax::Methods
before do
define_model('User')
define_model('Post', :user_id => :integer) do
belongs_to :user
end
def define_factories_with_method
FactoryGirl.define do
factory :user
@ -50,15 +44,48 @@ describe "a stubbed instance with :method => :build" do
end
end
subject { build_stubbed(:post) }
def define_factories_with_strategy
FactoryGirl.define do
factory :user
it "acts as if it is saved in the database" do
should_not be_new_record
factory :post do
association(:user, :strategy => :build)
end
end
end
it "assigns associations and acts as if it is saved" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
before do
define_model('User')
define_model('Post', :user_id => :integer) do
belongs_to :user
end
end
context "associations declared with :strategy" do
before { define_factories_with_strategy }
subject { build_stubbed(:post) }
it "acts as if it is saved in the database" do
should_not be_new_record
end
it "assigns associations and acts as if it is saved" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end
context "associations declared with :method" do
before { define_factories_with_method }
subject { build_stubbed(:post) }
it "acts as if it is saved in the database" do
should_not be_new_record
end
it "assigns associations and acts as if it is saved" do
subject.user.should be_kind_of(User)
subject.user.should_not be_new_record
end
end
end

View File

@ -9,23 +9,23 @@ describe FactoryGirl::AssociationRunner do
end
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")
FactoryGirl::AssociationRunner.new(:user, FactoryGirl::Strategy::Build, :strategy => :build, :name => "John").run
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
end
it "runs a strategy inferred by name based on a factory name" do
FactoryGirl::AssociationRunner.new(:user, :build, :method => :build, :name => "John").run
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :method => :build, :name => "John")
FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :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")
FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run(FactoryGirl::Strategy::Create)
factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :strategy => :build, :name => "John")
end
it "raises if the strategy cannot be inferred" do
expect do
FactoryGirl::AssociationRunner.new(:user, :bogus_strategy, :method => :build, :name => "John").run
FactoryGirl::AssociationRunner.new(:user, :bogus_strategy, :strategy => :build, :name => "John").run
end.to raise_error("unrecognized method bogus_strategy")
end
end

View File

@ -3,5 +3,5 @@ require "spec_helper"
describe FactoryGirl::Strategy::Build do
it_should_behave_like "strategy with association support", FactoryGirl::Strategy::Create
it_should_behave_like "strategy with callbacks", :after_build
it_should_behave_like "strategy with :method => :build", FactoryGirl::Strategy::Build
it_should_behave_like "strategy with :strategy => :build", FactoryGirl::Strategy::Build
end

View File

@ -3,7 +3,7 @@ require 'spec_helper'
describe FactoryGirl::Strategy::Stub do
it_should_behave_like "strategy with association support", FactoryGirl::Strategy::Stub
it_should_behave_like "strategy with callbacks", :after_stub
it_should_behave_like "strategy with :method => :build", FactoryGirl::Strategy::Stub
it_should_behave_like "strategy with :strategy => :build", FactoryGirl::Strategy::Stub
context "asking for a result" do
before { Timecop.freeze(Time.now) }

View File

@ -41,12 +41,12 @@ shared_examples_for "strategy with association support" do |factory_girl_strateg
end
end
shared_examples_for "strategy with :method => :build" do |factory_girl_strategy_class|
shared_examples_for "strategy with :strategy => :build" do |factory_girl_strategy_class|
let(:factory) { stub("associate_factory") }
def association_named(name, overrides)
strategy = overrides.delete(:method)
runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides)
strategy = overrides[:strategy] || overrides[:method]
runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides.except(:strategy, :method))
subject.association(runner)
end
@ -56,11 +56,21 @@ shared_examples_for "strategy with :method => :build" do |factory_girl_strategy_
end
it "runs the factory with the correct overrides" do
association_named(:author, :method => :build, :great => "value")
association_named(:author, :strategy => :build, :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, :strategy => :build, :great => "value")
FactoryGirl.should have_received(:factory_by_name).with(:author)
end
it "runs the factory with the correct overrides with :method" do
association_named(:author, :method => :build, :great => "value")
factory.should have_received(:run).with(factory_girl_strategy_class, { :great => "value" })
end
it "finds the factory with the correct factory name with :method" do
association_named(:author, :method => :build, :great => "value")
FactoryGirl.should have_received(:factory_by_name).with(:author)
end