public
Rubygem
Clone URL: git://github.com/thoughtbot/factory_girl.git
#1 - automatically load factory definitions from standard locations
Joe Ferris (author)
Wed Aug 20 07:20:41 -0700 2008
commit  30a877ed156d8563d0660b85a18d231a3224466d
tree    809e501128f29907a9e15dcc79ac1dee4aab6acf
parent  428c0b537d267ef8924533694d2d33844d818c17
...
12
13
14
 
 
...
12
13
14
15
16
0
@@ -12,3 +12,5 @@ require 'factory_girl/aliases'
0
 def Factory (name, attrs = {})
0
   Factory.create(name, attrs)
0
 end
0
+
0
+Factory.find_definitions
...
3
4
5
 
 
 
 
 
 
 
6
7
8
...
173
174
175
 
 
 
 
 
 
 
 
 
 
176
177
178
...
3
4
5
6
7
8
9
10
11
12
13
14
15
...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
0
@@ -3,6 +3,13 @@ class Factory
0
   cattr_accessor :factories #:nodoc:
0
   self.factories = {}
0
 
0
+ # An Array of strings specifying locations that should be searched for
0
+ # factory definitions. By default, factory_girl will attempt to require
0
+ # "factories," "test/factories," and "spec/factories." Only the first
0
+ # existing file will be loaded.
0
+ cattr_accessor :definition_file_paths
1
+ self.definition_file_paths = %w(factories test/factories spec/factories)
0
+
0
   attr_reader :factory_name
0
 
0
   # Defines a new factory that can be used by the build strategies (create and
0
@@ -173,6 +180,16 @@ class Factory
0
       factory_by_name(name).create(attrs)
0
     end
0
 
0
+ def find_definitions #:nodoc:
0
+ definition_file_paths.each do |path|
0
+ begin
0
+ require(path)
0
+ break
0
+ rescue LoadError
0
+ end
0
+ end
0
+ end
0
+
0
     private
0
 
0
     def factory_by_name (name)
...
415
416
417
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418
...
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
0
@@ -415,4 +415,20 @@ class FactoryTest < Test::Unit::TestCase
0
 
0
   end
0
 
0
+ Factory.definition_file_paths.each do |file|
0
+ should "automatically load definitions from #{file}.rb" do
0
+ Factory.stubs(:require).raises(LoadError)
0
+ Factory.expects(:require).with(file)
0
+ Factory.find_definitions
0
+ end
0
+ end
0
+
0
+ should "only load the first set of factories detected" do
0
+ first, second, third = Factory.definition_file_paths
0
+ Factory.expects(:require).with(first).raises(LoadError)
0
+ Factory.expects(:require).with(second)
0
+ Factory.expects(:require).with(third).never
0
+ Factory.find_definitions
0
+ end
0
+
0
 end

Comments

  • Since these are relative directories, they’d only work from the top level of a project.

    With rake, you can actually invoke it from any subdir of a project, and it will find the rakefile at the top level.

    You can also invoke a test directly with ruby from anywhere.

    In both cases, it would be relative to where they’re run from.