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:
parent
8ccf44fbd1
commit
5b7258706d
4 changed files with 66 additions and 18 deletions
8
init.rb
Normal file
8
init.rb
Normal 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
|
|
@ -12,5 +12,3 @@ require 'factory_girl/aliases'
|
||||||
def Factory (name, attrs = {})
|
def Factory (name, attrs = {})
|
||||||
Factory.create(name, attrs)
|
Factory.create(name, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
Factory.find_definitions
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -433,20 +433,60 @@ class FactoryTest < Test::Unit::TestCase
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Factory.definition_file_paths.each do |file|
|
def self.context_in_directory_with_files(*files)
|
||||||
should "automatically load definitions from #{file}.rb" do
|
context "in a directory with #{files.to_sentence}" do
|
||||||
Factory.stubs(:require).raises(LoadError)
|
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
|
||||||
|
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
should "only load the first set of factories detected" do
|
context_in_directory_with_files 'factories.rb' do
|
||||||
first, second, third = Factory.definition_file_paths
|
should_require_definitions_from 'factories.rb'
|
||||||
Factory.expects(:require).with(first).raises(LoadError)
|
end
|
||||||
Factory.expects(:require).with(second)
|
|
||||||
Factory.expects(:require).with(third).never
|
%w(spec test).each do |dir|
|
||||||
Factory.find_definitions
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue