From c47affb1aa0ab41c8433ee64a2f8937a79439d2b Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Fri, 22 Jul 2011 11:38:56 -0400 Subject: [PATCH] Requiring factory definition files now works in 1.9 outside of tests Ruby 1.9 doesn't automatically include . in the load path; if we do a require to a relative path (eg a file within spec or test), 1.9 will raise a LoadError because spec or test isn't normally in the load path. --- features/find_definitions.feature | 30 +++++++++++++++++++ .../step_definitions/factory_girl_steps.rb | 10 +++++++ lib/factory_girl/find_definitions.rb | 4 +-- spec/factory_girl/find_definitions_spec.rb | 20 ++++++------- 4 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 features/find_definitions.feature create mode 100644 features/step_definitions/factory_girl_steps.rb diff --git a/features/find_definitions.feature b/features/find_definitions.feature new file mode 100644 index 000000000..d24976d8d --- /dev/null +++ b/features/find_definitions.feature @@ -0,0 +1,30 @@ +Feature: Factory girl can find factory definitions correctly + Scenario: Find definitions with a path + Given a file named "awesome_factories.rb" with: + """ + FactoryGirl.define do + factory :awesome_category, :parent => :category do + name "awesome!!!" + end + end + """ + When "awesome_factories.rb" is added to Factory Girl's file definitions path + And I create a "awesome_category" instance from Factory Girl + Then I should find the following for the last category: + | name | + | awesome!!! | + + Scenario: Find definitions with a folder + Given a file named "nested/great_factories.rb" with: + """ + FactoryGirl.define do + factory :great_category, :parent => :category do + name "great!!!" + end + end + """ + When "nested" is added to Factory Girl's file definitions path + And I create a "great_category" instance from Factory Girl + Then I should find the following for the last category: + | name | + | great!!! | diff --git a/features/step_definitions/factory_girl_steps.rb b/features/step_definitions/factory_girl_steps.rb new file mode 100644 index 000000000..3af93a668 --- /dev/null +++ b/features/step_definitions/factory_girl_steps.rb @@ -0,0 +1,10 @@ +When /^"([^"]*)" is added to Factory Girl's file definitions path$/ do |file_name| + new_factory_file = File.join(current_dir, file_name.gsub(".rb", "")) + FactoryGirl.definition_file_paths ||= [] + FactoryGirl.definition_file_paths << new_factory_file + FactoryGirl.find_definitions +end + +When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name| + FactoryGirl.create(factory_name) +end diff --git a/lib/factory_girl/find_definitions.rb b/lib/factory_girl/find_definitions.rb index c4d4fb0dc..307fa1c31 100644 --- a/lib/factory_girl/find_definitions.rb +++ b/lib/factory_girl/find_definitions.rb @@ -11,11 +11,11 @@ class << self def self.find_definitions #:nodoc: definition_file_paths.each do |path| - require("#{path}.rb") if File.exists?("#{path}.rb") + require("./#{path}.rb") if File.exists?("#{path}.rb") if File.directory? path Dir[File.join(path, '**', '*.rb')].sort.each do |file| - require file + require "./#{file}" end end end diff --git a/spec/factory_girl/find_definitions_spec.rb b/spec/factory_girl/find_definitions_spec.rb index 6ec60a55f..1b0c11842 100644 --- a/spec/factory_girl/find_definitions_spec.rb +++ b/spec/factory_girl/find_definitions_spec.rb @@ -47,7 +47,7 @@ def self.in_directory_with_files(*files) describe "with factories.rb" do in_directory_with_files 'factories.rb' it_should_behave_like "finds definitions" do - it { should require_definitions_from('factories.rb') } + it { should require_definitions_from('./factories.rb') } end end @@ -55,14 +55,14 @@ def self.in_directory_with_files(*files) describe "with a factories file under #{dir}" do in_directory_with_files File.join(dir, 'factories.rb') it_should_behave_like "finds definitions" do - it { should require_definitions_from("#{dir}/factories.rb") } + it { should require_definitions_from("./#{dir}/factories.rb") } end end describe "with a factories file under #{dir}/factories" do in_directory_with_files File.join(dir, 'factories', 'post_factory.rb') it_should_behave_like "finds definitions" do - it { should require_definitions_from("#{dir}/factories/post_factory.rb") } + it { should require_definitions_from("./#{dir}/factories/post_factory.rb") } end end @@ -70,8 +70,8 @@ def self.in_directory_with_files(*files) in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') it_should_behave_like "finds definitions" do - it { should require_definitions_from("#{dir}/factories/post_factory.rb") } - it { should require_definitions_from("#{dir}/factories/person_factory.rb") } + it { should require_definitions_from("./#{dir}/factories/post_factory.rb") } + it { should require_definitions_from("./#{dir}/factories/person_factory.rb") } end end @@ -91,9 +91,9 @@ def self.in_directory_with_files(*files) File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') it_should_behave_like "finds definitions" do - it { should require_definitions_from("#{dir}/factories.rb") } - it { should require_definitions_from("#{dir}/factories/post_factory.rb") } - it { should require_definitions_from("#{dir}/factories/person_factory.rb") } + it { should require_definitions_from("./#{dir}/factories.rb") } + it { should require_definitions_from("./#{dir}/factories/post_factory.rb") } + it { should require_definitions_from("./#{dir}/factories/person_factory.rb") } end end @@ -101,8 +101,8 @@ def self.in_directory_with_files(*files) in_directory_with_files File.join(dir, 'factories', 'subdirectory', 'post_factory.rb'), File.join(dir, 'factories', 'subdirectory', 'person_factory.rb') it_should_behave_like "finds definitions" do - it { should require_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") } - it { should require_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") } + it { should require_definitions_from("./#{dir}/factories/subdirectory/post_factory.rb") } + it { should require_definitions_from("./#{dir}/factories/subdirectory/person_factory.rb") } end end end