Skip to content

Commit

Permalink
Deprecate :method in favor of :strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Feb 17, 2012
1 parent d0a253f commit 791591b
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 41 deletions.
4 changes: 2 additions & 2 deletions GETTING_STARTED.md
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/factory_girl/evaluator.rb
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/build_spec.rb
Expand Up @@ -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
Expand All @@ -45,7 +45,7 @@
factory :user

factory :post do
association(:user, :method => :build)
association(:user, :strategy => :build)
end
end
end
Expand Down
49 changes: 39 additions & 10 deletions spec/acceptance/create_spec.rb
Expand Up @@ -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

Expand Down
53 changes: 40 additions & 13 deletions spec/acceptance/stub_spec.rb
Expand Up @@ -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
14 changes: 7 additions & 7 deletions spec/factory_girl/association_runner_spec.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/factory_girl/strategy/build_spec.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/factory_girl/strategy/stub_spec.rb
Expand Up @@ -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) }
Expand Down
18 changes: 14 additions & 4 deletions spec/support/shared_examples/strategy.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 791591b

Please sign in to comment.