can't run unit tests, not even with "ruby -I test"

Trying to run unit test in a new rails 3 project. When I follow
instructions recommended by ruby on rails guides page
(http://guides.rubyonrails.org/testing.html) I get the following:

ruby test/unit/schedule_weekly_test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to
load -- test_helper (LoadError)

After much googling, everyone seems to recommend that the 'fix' is to
run a test like so:

ruby -I test test/unit/whatever.rb

That it's impossible to run an autogenerated test using the method
recommended on the guides page is pretty annoying. At any rate, when I
run it with -I test, I no longer get the test_helper errors, but I get a
different one:

activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require':
no such file to load -- app/models/users/user (LoadError)

I have a users folder in my model path, that I load with:

config.autoload_paths += %W(/app/model/users)

in application.rb. Any ideas for what I can do to let my tests find the
files within this folder?

Ben Porterfield wrote in post #960978:

Trying to run unit test in a new rails 3 project.

RSpec's a lot nicer; I'd recommend that instead. But...

When I follow
instructions recommended by ruby on rails guides page
(http://guides.rubyonrails.org/testing.html)

How are you following those instructions? I think that page recommends
using the rake tasks, not running the tests as you've indicated.

I get the following:

ruby test/unit/schedule_weekly_test.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to
load -- test_helper (LoadError)

After much googling, everyone seems to recommend that the 'fix' is to
run a test like so:

ruby -I test test/unit/whatever.rb

That it's impossible to run an autogenerated test using the method
recommended on the guides page is pretty annoying. At any rate, when I
run it with -I test, I no longer get the test_helper errors, but I get a
different one:

activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require':
no such file to load -- app/models/users/user (LoadError)

I have a users folder in my model path, that I load with:

config.autoload_paths += %W(/app/model/users)

in application.rb. Any ideas for what I can do to let my tests find the
files within this folder?

Use the Rake tasks. They should set up everything correctly. I also
highly recommend the use of Autotest.

Best,

Ben Porterfield wrote in post #960978:

Trying to run unit test in a new rails 3 project.

RSpec's a lot nicer; I'd recommend that instead. But...

When I follow
instructions recommended by ruby on rails guides page
(http://guides.rubyonrails.org/testing.html)

How are you following those instructions? I think that page recommends
using the rake tasks, not running the tests as you've indicated.

The OP is correct, section 3.2 of the guide shows tests being run
directly by ruby (without -I)

Colin

Colin Law wrote in post #961100:

using the rake tasks, not running the tests as you've indicated.

The OP is correct, section 3.2 of the guide shows tests being run
directly by ruby (without -I)

I just looked. It seems to only show -I.

Colin

Best,

From http://guides.rubyonrails.org/testing.html#running-tests

3.2 Running Tests
Running a test is as simple as invoking the file containing the test
cases through Ruby:
$ cd test
$ ruby unit/post_test.rb

Colin

Trying to run unit test in a new rails 3 project. When I follow

instructions recommended by ruby on rails guides page

(http://guides.rubyonrails.org/testing.html) I get the following:

ruby test/unit/schedule_weekly_test.rb

internal:lib/rubygems/custom_require:29:in `require’: no such file to

load – test_helper (LoadError)

After much googling, everyone seems to recommend that the ‘fix’ is to

run a test like so:

ruby -I test test/unit/whatever.rb

That it’s impossible to run an autogenerated test using the method

recommended on the guides page is pretty annoying. At any rate, when I

run it with -I test, I no longer get the test_helper errors, but I get a

different one:

activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require’:

no such file to load – app/models/users/user (LoadError)

I have a users folder in my model path, that I load with:

config.autoload_paths += %W(/app/model/users)

in application.rb. Any ideas for what I can do to let my tests find the

files within this folder?

This is a shot in the dark, but I recently upgraded an project to Rails 3 and the tests would just not run - no error, just a new command prompt after a pause. I solved by using the process of elimination to comment out gems in my gem file — I think if I remember right I found the culprit to be ‘cucumber’ (not ‘cucumber_rails’). Once I commented this my tests started working. And ‘cucumber’ is not necessary to run ‘cucumber_rails’, so all ended well. So just an idea to look at your gemfile if you have added anything.

So, Colin, you are correct, I'm reading the instructions exactly from
the rails guide. Like I said, following their instructions exactly gives
me a ridiculous error, modifying them slightly lets the test at least
try to run.

Regardless of whether I run rake test or ruby -I test test/unit/blah.rb,
I still receive the same error I've mentioned above - rails says:

in `require': no such file to load -- app/models/users/user (LoadError)

Strange to me that it manages to know where the model is
(app/models/users/user) but then when trying to load says 'no such file
to load'.

David, thanks for the attempt but this is a really fresh project, all
rails 3 from the start, and without either of those gems.

Any other ideas?

Update.

So upon further investigation it appears as though the issue comes from
my model directory structure. I have a lot of models, so I've placed
some into folders (but not namespaced), and auto-loaded the models using
something like:

config.autoload_paths += %W(app/models/users/)

Apparently, this works great in development, but breaks down in test or
production environments. My first question is - does anyone know why?

Although the users folder contains quite a few models, the only one that
appears to break anything is the User model. If I pull the user.rb class
up one level into app/models, not only does everything seem to run, but
I can also still access the other models that are in the
app/models/users directory. So, my second question is - why just the
users model?

Ben Porterfield wrote in post #961247:

Update.

So upon further investigation it appears as though the issue comes from
my model directory structure. I have a lot of models, so I've placed
some into folders (but not namespaced),

This may be your problem, but in any case, it's terrible practice. If
you're using directories, you should always use namespaces to match.
There's no advantage to breaking Rails' conventions here.

Best,

So I'm new to rails but most of what I read actually point the other way
- that namespaced models are mostly broken in rails, and to never use
them. As such, I'm following a few other suggestions I've read to use
autoload to load models organized into subdirectories. This is the cause
of my issues, however, so I suppose I'll pull all my models into the
models folder.

Ben Porterfield wrote in post #961261:

So I'm new to rails but most of what I read actually point the other way
- that namespaced models are mostly broken in rails, and to never use
them.

Then you've got bad advice.

As such, I'm following a few other suggestions I've read to use
autoload to load models organized into subdirectories. This is the cause
of my issues, however, so I suppose I'll pull all my models into the
models folder.

Just use namespaces properly!

Best,