# Require factory fails if I am not using rake to run tests

**URL:** <https://www.ruby-forum.com/t/require-factory-fails-if-i-am-not-using-rake-to-run-tests/137423>\
**Category:** Rails\
**Created:** [May 13, 2008, 1:18am UTC](https://www.ruby-forum.com/t/require-factory-fails-if-i-am-not-using-rake-to-run-tests/137423 "2008-05-13T01:18:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [May 13, 2008, 1:18am UTC](https://www.ruby-forum.com/t/require-factory-fails-if-i-am-not-using-rake-to-run-tests/137423/1 "2008-05-13T01:18:43Z")

</div>

I am trying to reuse some code from a Rails 1.2.6 app in edge rails.  
In the old app, I can run all my tests with ‘rake’ or I can run  
individual tests using ‘ruby test/functionals/foo\_controller\_test.rb’.  
But in my edge rails app, I have to use ‘rake’. If I try to run  
individual tests, I get the error below. How can include my factory  
module in a way that lets me run tests via rake AND individually with  
‘ruby $filename’?

/usr/bin/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require': no such file to load -- factory (MissingSourceFile) from /usr/bin/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`require’  
from  
/rails/cms/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:495:in  
`require' from /rails/cms/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:342:in`new\_constants\_in’  
from  
/rails/cms/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:495:in  
`require’  
from ./test/unit/…/test\_helper.rb:5  
from test/unit/user\_test.rb:1

The desired Factory module is in test/factory.rb - right next to  
test\_helper.rb. The offending includes are:

From test/unit/user\_test.rb

require File.dirname( **FILE** ) + ‘/…/test\_helper’  
class UserTest \< Test::Unit::TestCase

From tests/test\_helper.rb

ENV[“RAILS\_ENV”] = “test”  
require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)  
require ‘test\_help’  
require ‘factory’  
class Test::Unit::TestCase

And test/factory.rb starts

module Factory …

–  
Cynthia

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [May 13, 2008, 1:32am UTC](https://www.ruby-forum.com/t/require-factory-fails-if-i-am-not-using-rake-to-run-tests/137423/2 "2008-05-13T01:32:45Z")

</div>

Cynthia K. wrote:

> require File.expand\_path(File.dirname( **FILE** ) + “/…/config/environment”)  
> require ‘test\_help’  
> require ‘factory’

I thought newer Railses reduced their file lookup paths. Use the  
dirname( **FILE** ) trick you see right above your factory!

> require File.expand\_path(File.dirname( **FILE** ) + “/…/factory.rb”)

Alternately, use RAILS\_ROOT, because environment.rb sets that.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [January 20, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/require-factory-fails-if-i-am-not-using-rake-to-run-tests/137423/3 "2009-01-20T22:15:55Z")

</div>

Phlip wrote:

> Cynthia K. wrote:
> 
> > require File.expand\_path(File.dirname( **FILE** ) + “/…/config/environment”)  
> > require ‘test\_help’  
> > require ‘factory’
> 
> I thought newer Railses reduced their file lookup paths. Use the  
> dirname( **FILE** ) trick you see right above your factory!
> 
> > require File.expand\_path(File.dirname( **FILE** ) + “/…/factory.rb”)
> 
> Alternately, use RAILS\_ROOT, because environment.rb sets that.
> 
> –  
> Phlip

Why does it fail to find ‘factory.rb’, though? Shouldn’t a ‘require’,  
written in file A, find a co-located file B, regardless of the loadpath?

It may be desirable to avoid the RAILS\_ROOT and **FILE** versions of  
‘require’ to avoid loading the file multiple times, unnecessarily. For  
example, you might get warnings about ‘redefined’ constants.
