diff --git a/features/find_definitions.feature b/features/find_definitions.feature index 63032323c..45cf2c820 100644 --- a/features/find_definitions.feature +++ b/features/find_definitions.feature @@ -3,7 +3,7 @@ Feature: Factory girl can find factory definitions correctly Given a file named "awesome_factories.rb" with: """ FactoryGirl.define do - factory :awesome_category, :parent => :category do + factory :awesome_category, :class => Category do name "awesome!!!" end end @@ -18,13 +18,13 @@ Feature: Factory girl can find factory definitions correctly Given a file named "awesome_factories.rb" with: """ FactoryGirl.define do - factory :awesome_category, :parent => :category do + factory :another_awesome_category, :class => Category do name "awesome!!!" end end """ When "awesome_factories.rb" is added to Factory Girl's file definitions path as an absolute path - And I create a "awesome_category" instance from Factory Girl + And I create a "another_awesome_category" instance from Factory Girl Then I should find the following for the last category: | name | | awesome!!! | @@ -33,7 +33,7 @@ Feature: Factory girl can find factory definitions correctly Given a file named "nested/great_factories.rb" with: """ FactoryGirl.define do - factory :great_category, :parent => :category do + factory :great_category, :class => Category do name "great!!!" end end @@ -43,3 +43,20 @@ Feature: Factory girl can find factory definitions correctly Then I should find the following for the last category: | name | | great!!! | + + Scenario: Find definitions after clearing loaded factories + Given a file named "nested/swell_factories.rb" with: + """ + FactoryGirl.define do + factory :swell_category, :class => Category do + name "swell!!!" + end + end + """ + When "nested" is added to Factory Girl's file definitions path + And I clear out the factories + And I find definitions + And I create a "swell_category" instance from Factory Girl + Then I should find the following for the last category: + | name | + | swell!!! | diff --git a/features/step_definitions/factory_girl_steps.rb b/features/step_definitions/factory_girl_steps.rb index f03c15183..8c1129ec9 100644 --- a/features/step_definitions/factory_girl_steps.rb +++ b/features/step_definitions/factory_girl_steps.rb @@ -1,16 +1,26 @@ +module FactoryGirlDefinitionsHelper + def append_file_to_factory_girl_definitions_path(path_to_file) + FactoryGirl.definition_file_paths ||= [] + FactoryGirl.definition_file_paths << path_to_file + end +end + +World(FactoryGirlDefinitionsHelper) + 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 + + append_file_to_factory_girl_definitions_path(new_factory_file) + + When %{I find definitions} end When /^"([^"]*)" is added to Factory Girl's file definitions path as an absolute path$/ do |file_name| new_factory_file = File.expand_path(File.join(current_dir, file_name.gsub(".rb", ""))) - FactoryGirl.definition_file_paths ||= [] - FactoryGirl.definition_file_paths << new_factory_file - FactoryGirl.find_definitions + append_file_to_factory_girl_definitions_path(new_factory_file) + + When %{I find definitions} end When /^I create a "([^"]*)" instance from Factory Girl$/ do |factory_name| @@ -23,3 +33,11 @@ new_table = Cucumber::Ast::Table.new([headers] + rows) Given %{the following person exists:}, new_table end + +When /^I clear out the factories$/ do + FactoryGirl.factories.clear +end + +When /^I find definitions$/ do + FactoryGirl.find_definitions +end diff --git a/lib/factory_girl/find_definitions.rb b/lib/factory_girl/find_definitions.rb index 0c6dc9be7..f00d3a3da 100644 --- a/lib/factory_girl/find_definitions.rb +++ b/lib/factory_girl/find_definitions.rb @@ -10,14 +10,14 @@ class << self self.definition_file_paths = %w(factories test/factories spec/factories) def self.find_definitions #:nodoc: - definition_file_paths.each do |path| - path = File.expand_path(path) + absolute_definition_file_paths = definition_file_paths.map {|path| File.expand_path(path) } - require("#{path}.rb") if File.exists?("#{path}.rb") + absolute_definition_file_paths.uniq.each do |path| + load("#{path}.rb") if File.exists?("#{path}.rb") if File.directory? path Dir[File.join(path, '**', '*.rb')].sort.each do |file| - require file + load file end end end diff --git a/spec/factory_girl/find_definitions_spec.rb b/spec/factory_girl/find_definitions_spec.rb index 6daec8f80..9de7ccf80 100644 --- a/spec/factory_girl/find_definitions_spec.rb +++ b/spec/factory_girl/find_definitions_spec.rb @@ -2,20 +2,20 @@ share_examples_for "finds definitions" do before do - stub(FactoryGirl).require + stub(FactoryGirl).load FactoryGirl.find_definitions end subject { FactoryGirl } end -RSpec::Matchers.define :require_definitions_from do |file| +RSpec::Matchers.define :load_definitions_from do |file| match do |given| - @has_received = have_received.method_missing(:require, File.expand_path(file)) + @has_received = have_received.method_missing(:load, File.expand_path(file)) @has_received.matches?(given) end description do - "require definitions from #{file}" + "load definitions from #{file}" end failure_message_for_should do @@ -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 load_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 load_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 load_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 load_definitions_from("#{dir}/factories/post_factory.rb") } + it { should load_definitions_from("#{dir}/factories/person_factory.rb") } end end @@ -80,7 +80,7 @@ def self.in_directory_with_files(*files) File.join(dir, 'factories', 'a.rb') it "should load the files in the right order" do @loaded = [] - stub(FactoryGirl).require { |a| @loaded << File.split(a)[-1] } + stub(FactoryGirl).load { |a| @loaded << File.split(a)[-1] } FactoryGirl.find_definitions @loaded.should == ["a.rb", "b.rb"] 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 load_definitions_from("#{dir}/factories.rb") } + it { should load_definitions_from("#{dir}/factories/post_factory.rb") } + it { should load_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 load_definitions_from("#{dir}/factories/subdirectory/post_factory.rb") } + it { should load_definitions_from("#{dir}/factories/subdirectory/person_factory.rb") } end end end