# Generated test files and bad require 'test\_helper'

**URL:** <https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005>\
**Category:** Rails\
**Created:** [May 14, 2009, 10:23pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005 "2009-05-14T22:23:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 10:23pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/1 "2009-05-14T22:23:27Z")

</div>

Why do the generated test files have: require ‘test\_helper’

It cannot obviously find the file because it’s located in a  
subdirectory. That’s annoying.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 10:36pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/2 "2009-05-14T22:36:21Z")

</div>

It finds it ok for me when run from rake test but not from ruby  
test/unit/test.rb  
I change all the require lines to

require File.dirname( **FILE** ) + ‘/…/test\_helper’

I suspect there is a better way of achieving this effect

Colin

2009/5/14 Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 10:50pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/3 "2009-05-14T22:50:34Z")

</div>

Colin L. wrote:

> It finds it ok for me when run from rake test but not from ruby  
> test/unit/test.rb  
> I change all the require lines to
> 
> require File.dirname( **FILE** ) + ‘/…/test\_helper’
> 
> I suspect there is a better way of achieving this effect  
> Thanks Colin for your input. Actually it works the same for me (I never  
> noticed), but I often find myself only running a little set of tests and  
> not always the full rake test.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/4 "2009-05-14T23:21:33Z")

</div>

Fernando P. wrote:

> Colin L. wrote:

> > require File.dirname( **FILE** ) + ‘/…/test\_helper’
> > 
> > I suspect there is a better way of achieving this effect

> Thanks Colin for your input. Actually it works the same for me (I never  
> noticed), but I often find myself only running a little set of tests and  
> not always the full rake test.

Some editors run tests in the “wrong” folder - if they can run unit  
tests at all

- so that File.dirname mishmash is crucial for Rails’s tests. No idea  
why the  
newer version took it out!

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 11:53pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/5 "2009-05-14T23:53:10Z")

</div>

Frederick C. wrote:

> > Some editors run tests in the “wrong” folder - if they can run unit tests at all
> > 
> > - so that File.dirname mishmash is crucial for Rails’s tests. No idea why the  
> > newer version took it out!
> 
> Take a look at the comments for [Generated tests rely on test dir in load path rather than File.dirnam… · rails/rails@e817080 · GitHub](http://github.com/rails/rails/commit/e8170805df1a32119db9d328daee1239b338ac71)

“different relative requires of the same file cause Ruby to load it  
multiple  
times. This causes bugs. Fixing it is the point of this change.”

Then use File.expand\_path(File.dirname( **FILE** ) +  
‘/…/test\_helper.rb’).

expand\_path - if I spelled it right - goes to the OS filesystem for the  
uniquely  
correct path, right?

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 11:43pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/6 "2009-05-14T23:43:13Z")

</div>

On May 14, 10:20 pm, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - so that File.dirname mishmash is crucial for Rails’s tests. No idea why the  
> newer version took it out!

Take a look at the comments for

> <https://github.com/rails/rails/commit/e8170805df1a32119db9d328daee1239b338ac71>
>
> git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9133 5ecf4fe2-1ee6-031…0-87b1-e25e094e27de

Fred

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [May 14, 2009, 11:56pm UTC](https://www.ruby-forum.com/t/generated-test-files-and-bad-require-test-helper/168005/7 "2009-05-14T23:56:06Z")

</div>

> “different relative requires of the same file cause Ruby to load it multiple  
> times. This causes bugs. Fixing it is the point of this change.”
> 
> Then use File.expand\_path(File.dirname( **FILE** ) + ‘/…/test\_helper.rb’).
> 
> expand\_path - if I spelled it right - goes to the OS filesystem for the uniquely  
> correct path, right?

“File.expand\_path doesn’t behave on Windows”. It always comes back to  
that, huh? (-:

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)
