1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Don't always find factory definitions, as that can be problematic (see http://groups.google.com/group/factory_girl/browse_thread/thread/a6e4d67974fa214?hl=en). Added init.rb to do rails specific factory loading. Factory.find_definition will also check subdirectories for definitions to load.

This commit is contained in:
Josh Nichols 2008-10-24 12:14:13 -04:00 committed by Joe Ferris
parent 8ccf44fbd1
commit 5b7258706d
4 changed files with 66 additions and 18 deletions

8
init.rb Normal file
View file

@ -0,0 +1,8 @@
config.after_initialize do
require 'factory_girl'
Factory.definition_file_paths = [
File.join(RAILS_ROOT, 'test', 'factories'),
File.join(RAILS_ROOT, 'spec', 'factories')
]
Factory.find_definitions
end

View file

@ -11,6 +11,4 @@ require 'factory_girl/aliases'
# Factory(:user, :name => 'Joe') # Factory(:user, :name => 'Joe')
def Factory (name, attrs = {}) def Factory (name, attrs = {})
Factory.create(name, attrs) Factory.create(name, attrs)
end end
Factory.find_definitions

View file

@ -182,10 +182,12 @@ class Factory
def find_definitions #:nodoc: def find_definitions #:nodoc:
definition_file_paths.each do |path| definition_file_paths.each do |path|
begin require("#{path}.rb") if File.exists?("#{path}.rb")
require(path)
break if File.directory? path
rescue LoadError Dir[File.join(path, '*.rb')].each do |file|
require file
end
end end
end end
end end

View file

@ -432,21 +432,61 @@ class FactoryTest < Test::Unit::TestCase
end end
end end
def self.context_in_directory_with_files(*files)
context "in a directory with #{files.to_sentence}" do
setup do
@pwd = Dir.pwd
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
FileUtils.mkdir_p @tmp_dir
Dir.chdir(@tmp_dir)
files.each do |file|
FileUtils.mkdir_p File.dirname(file)
FileUtils.touch file
Factory.stubs(:require).with(file)
end
end
teardown do
Dir.chdir(@pwd)
FileUtils.rm_rf(@tmp_dir)
end
Factory.definition_file_paths.each do |file| yield
should "automatically load definitions from #{file}.rb" do end
Factory.stubs(:require).raises(LoadError) end
def self.should_require_definitions_from(file)
should "load definitions from #{file}" do
Factory.expects(:require).with(file) Factory.expects(:require).with(file)
Factory.find_definitions Factory.find_definitions
end
end
context_in_directory_with_files 'factories.rb' do
should_require_definitions_from 'factories.rb'
end
%w(spec test).each do |dir|
context_in_directory_with_files File.join(dir, 'factories.rb') do
should_require_definitions_from "#{dir}/factories.rb"
end
context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb') do
should_require_definitions_from "#{dir}/factories/post_factory.rb"
end
context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do
should_require_definitions_from "#{dir}/factories/post_factory.rb"
should_require_definitions_from "#{dir}/factories/person_factory.rb"
end
context_in_directory_with_files File.join(dir, 'factories.rb'), File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do
should_require_definitions_from "#{dir}/factories.rb"
should_require_definitions_from "#{dir}/factories/post_factory.rb"
should_require_definitions_from "#{dir}/factories/person_factory.rb"
end end
end end
should "only load the first set of factories detected" do
first, second, third = Factory.definition_file_paths
Factory.expects(:require).with(first).raises(LoadError)
Factory.expects(:require).with(second)
Factory.expects(:require).with(third).never
Factory.find_definitions
end
end end