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

**URL:** <https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534>\
**Category:** rubyonrails-talk\
**Created:** [May 14, 2009, 8:23pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534 "2009-05-14T20:23:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 14, 2009, 8:23pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/1 "2009-05-14T20: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:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 14, 2009, 8:35pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/2 "2009-05-14T20:35:36Z")

</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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 14, 2009, 8:50pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/3 "2009-05-14T20:50:34Z")

</div>

Colin Law 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:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [May 14, 2009, 9:20pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/4 "2009-05-14T21:20:35Z")

</div>

Fernando Perez wrote:

> Colin Law 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!

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 14, 2009, 9:42pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/5 "2009-05-14T21:42:35Z")

</div>

> Fernando Perez wrote: \> Colin Law 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!

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)

Fred

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [May 14, 2009, 9:51pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/6 "2009-05-14T21:51:56Z")

</div>

Frederick Cheung 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?

---

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [May 14, 2009, 9:53pm UTC](https://discuss.rubyonrails.org/t/generated-test-files-and-bad-require-test-helper/42534/7 "2009-05-14T21:53:37Z")

</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? (-:
