From 4d8d4193759979a18a57da1501b2435ad8ed9314 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Wed, 29 Jun 2011 14:43:15 -0400 Subject: [PATCH] Don't require a block to be passed when defining a factory --- lib/factory_girl/syntax/default.rb | 2 +- spec/acceptance/attribute_aliases_spec.rb | 3 +-- spec/acceptance/attributes_for_spec.rb | 3 +-- spec/acceptance/build_spec.rb | 3 +-- spec/acceptance/build_stubbed_spec.rb | 3 +-- spec/acceptance/create_spec.rb | 3 +-- spec/acceptance/default_strategy_spec.rb | 6 ++---- spec/acceptance/definition_spec.rb | 3 +-- spec/acceptance/definition_without_block_spec.rb | 16 ++++++++++++++++ 9 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 spec/acceptance/definition_without_block_spec.rb diff --git a/lib/factory_girl/syntax/default.rb b/lib/factory_girl/syntax/default.rb index b952cdc..2f9aa69 100644 --- a/lib/factory_girl/syntax/default.rb +++ b/lib/factory_girl/syntax/default.rb @@ -15,7 +15,7 @@ module FactoryGirl def factory(name, options = {}, &block) factory = Factory.new(name, options) proxy = FactoryGirl::DefinitionProxy.new(factory) - proxy.instance_eval(&block) + proxy.instance_eval(&block) if block_given? if parent = options.delete(:parent) factory.inherit_from(FactoryGirl.factory_by_name(parent)) end diff --git a/spec/acceptance/attribute_aliases_spec.rb b/spec/acceptance/attribute_aliases_spec.rb index 0e150cf..2d2c756 100644 --- a/spec/acceptance/attribute_aliases_spec.rb +++ b/spec/acceptance/attribute_aliases_spec.rb @@ -10,8 +10,7 @@ describe "attribute aliases" do end FactoryGirl.define do - factory :user do - end + factory :user factory :post do user diff --git a/spec/acceptance/attributes_for_spec.rb b/spec/acceptance/attributes_for_spec.rb index d8cda1c..8d2138c 100644 --- a/spec/acceptance/attributes_for_spec.rb +++ b/spec/acceptance/attributes_for_spec.rb @@ -15,8 +15,7 @@ describe "a generated attributes hash" do end FactoryGirl.define do - factory :user do - end + factory :user factory :post do title { "default title" } diff --git a/spec/acceptance/build_spec.rb b/spec/acceptance/build_spec.rb index 2194191..da50a69 100644 --- a/spec/acceptance/build_spec.rb +++ b/spec/acceptance/build_spec.rb @@ -12,8 +12,7 @@ describe "a built instance" do end FactoryGirl.define do - factory :user do - end + factory :user factory :post do user diff --git a/spec/acceptance/build_stubbed_spec.rb b/spec/acceptance/build_stubbed_spec.rb index ce0406d..60c86c5 100644 --- a/spec/acceptance/build_stubbed_spec.rb +++ b/spec/acceptance/build_stubbed_spec.rb @@ -14,8 +14,7 @@ describe "a generated stub instance" do end FactoryGirl.define do - factory :user do - end + factory :user factory :post do title { "default title" } diff --git a/spec/acceptance/create_spec.rb b/spec/acceptance/create_spec.rb index 624df73..92a88ca 100644 --- a/spec/acceptance/create_spec.rb +++ b/spec/acceptance/create_spec.rb @@ -12,8 +12,7 @@ describe "a created instance" do end FactoryGirl.define do - factory :user do - end + factory :user factory :post do user diff --git a/spec/acceptance/default_strategy_spec.rb b/spec/acceptance/default_strategy_spec.rb index 9127701..e06dc77 100644 --- a/spec/acceptance/default_strategy_spec.rb +++ b/spec/acceptance/default_strategy_spec.rb @@ -6,8 +6,7 @@ describe "default strategy" do define_model('User') FactoryGirl.define do - factory :user do - end + factory :user end Factory(:user).should_not be_new_record @@ -17,8 +16,7 @@ describe "default strategy" do define_model('User') FactoryGirl.define do - factory :user, :default_strategy => :build do - end + factory :user, :default_strategy => :build end Factory(:user).should be_new_record diff --git a/spec/acceptance/definition_spec.rb b/spec/acceptance/definition_spec.rb index 21276c0..0f4ce51 100644 --- a/spec/acceptance/definition_spec.rb +++ b/spec/acceptance/definition_spec.rb @@ -6,8 +6,7 @@ describe "an instance generated by a factory with a custom class name" do define_model("User", :admin => :boolean) FactoryGirl.define do - factory :user do - end + factory :user factory :admin, :class => User do admin { true } diff --git a/spec/acceptance/definition_without_block_spec.rb b/spec/acceptance/definition_without_block_spec.rb new file mode 100644 index 0000000..e37b0cb --- /dev/null +++ b/spec/acceptance/definition_without_block_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' +require 'acceptance/acceptance_helper' + +describe "an instance generated by a factory" do + before do + define_model("User") + + FactoryGirl.define do + factory :user + end + end + + it "registers the user factory" do + FactoryGirl.factory_by_name(:user).should be_a(FactoryGirl::Factory) + end +end