From 791591bd58874356858cb5eddce39e9432e65b4e Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Fri, 17 Feb 2012 11:29:11 -0500 Subject: [PATCH] Deprecate :method in favor of :strategy --- GETTING_STARTED.md | 4 +- lib/factory_girl/evaluator.rb | 9 +++- spec/acceptance/build_spec.rb | 4 +- spec/acceptance/create_spec.rb | 49 ++++++++++++++---- spec/acceptance/stub_spec.rb | 53 +++++++++++++++----- spec/factory_girl/association_runner_spec.rb | 14 +++--- spec/factory_girl/strategy/build_spec.rb | 2 +- spec/factory_girl/strategy/stub_spec.rb | 2 +- spec/support/shared_examples/strategy.rb | 18 +++++-- 9 files changed, 114 insertions(+), 41 deletions(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index a27397b11..db7cf71fb 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -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 diff --git a/lib/factory_girl/evaluator.rb b/lib/factory_girl/evaluator.rb index d3b00a782..3f3052431 100644 --- a/lib/factory_girl/evaluator.rb +++ b/lib/factory_girl/evaluator.rb @@ -31,7 +31,14 @@ def initialize(build_strategy, overrides = {}) 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 diff --git a/spec/acceptance/build_spec.rb b/spec/acceptance/build_spec.rb index 884e9dbcf..71698296e 100644 --- a/spec/acceptance/build_spec.rb +++ b/spec/acceptance/build_spec.rb @@ -31,7 +31,7 @@ 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 @@ factory :user factory :post do - association(:user, :method => :build) + association(:user, :strategy => :build) end end end diff --git a/spec/acceptance/create_spec.rb b/spec/acceptance/create_spec.rb index db43d1c20..7eb152d85 100644 --- a/spec/acceptance/create_spec.rb +++ b/spec/acceptance/create_spec.rb @@ -31,30 +31,59 @@ 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') + def define_factories_with_method + FactoryGirl.define do + factory :user - define_model('Post', :user_id => :integer) do - belongs_to :user + factory :post do + association(:user, :method => :build) + end end + end + def define_factories_with_strategy FactoryGirl.define do factory :user factory :post do - association(:user, :method => :build) + association(:user, :strategy => :build) end end end - subject { create('post') } + before do + define_model('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 + 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 diff --git a/spec/acceptance/stub_spec.rb b/spec/acceptance/stub_spec.rb index 2efa96137..7b39ba329 100644 --- a/spec/acceptance/stub_spec.rb +++ b/spec/acceptance/stub_spec.rb @@ -31,34 +31,61 @@ 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') + def define_factories_with_method + FactoryGirl.define do + factory :user - define_model('Post', :user_id => :integer) do - belongs_to :user + factory :post do + association(:user, :method => :build) + end end + end + def define_factories_with_strategy FactoryGirl.define do factory :user factory :post do - association(:user, :method => :build) + association(:user, :strategy => :build) end end end - subject { build_stubbed(:post) } - - it "acts as if it is saved in the database" do - should_not be_new_record + before do + define_model('User') + define_model('Post', :user_id => :integer) do + belongs_to :user + 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 + 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 diff --git a/spec/factory_girl/association_runner_spec.rb b/spec/factory_girl/association_runner_spec.rb index 713711b1e..53325e2a3 100644 --- a/spec/factory_girl/association_runner_spec.rb +++ b/spec/factory_girl/association_runner_spec.rb @@ -9,23 +9,23 @@ 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 diff --git a/spec/factory_girl/strategy/build_spec.rb b/spec/factory_girl/strategy/build_spec.rb index 183c1f58d..91bae271b 100644 --- a/spec/factory_girl/strategy/build_spec.rb +++ b/spec/factory_girl/strategy/build_spec.rb @@ -3,5 +3,5 @@ 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 diff --git a/spec/factory_girl/strategy/stub_spec.rb b/spec/factory_girl/strategy/stub_spec.rb index d26cc9ab7..a41908620 100644 --- a/spec/factory_girl/strategy/stub_spec.rb +++ b/spec/factory_girl/strategy/stub_spec.rb @@ -3,7 +3,7 @@ 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) } diff --git a/spec/support/shared_examples/strategy.rb b/spec/support/shared_examples/strategy.rb index 18102c809..92842ba02 100644 --- a/spec/support/shared_examples/strategy.rb +++ b/spec/support/shared_examples/strategy.rb @@ -41,12 +41,12 @@ def association_named(name, strategy, overrides) 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 @@ def association_named(name, overrides) 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