PlankTon
PlankTon

Reputation: 12605

Rails tests can't find test_helper

I'm trying to run individual tests through ruby test/unit/mytest.rb, but I always get a "no such file to load - test_helper" error. Google brought up a few suggestions, but none of them worked for me. I'm running Rails 3.0, Ruby 1.9.2 (through RVM) on Ubuntu 10.10

Here's what I've tried so far - any suggestions really appreciated

Any suggestions very appreciated - I'm stumped.

Upvotes: 29

Views: 21258

Answers (6)

Redrick
Redrick

Reputation: 466

I was fighting this thing myself today and i dislike the big require with whole path to file and stuff...

In my case it was fault of Rakefile..

so now it looks like this:

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |t|
  t.libs << "lib"
  t.libs << "test" # here is the test_helper
  t.pattern = "test/**/*_test.rb"
end

task default: :test

I know its old and has answer marked accepted, but maybe this will also help someone :) have a nice day

Upvotes: 15

Taylored Web Sites
Taylored Web Sites

Reputation: 1027

If you are creating a gem or engine, running rake test in the test dummy application directory will cause this error. Running rake test in the root of the gem will avoid this.

Upvotes: 4

jpgeek
jpgeek

Reputation: 5291

Rails 1.9 no longer includes the current directory in the LOAD_PATH, which causes this problem. You have a few options.

  1. call the test with the -I option from the app dir:

    ruby -I test test/functional/test_foo.rb

and use a require with no path:

require "test_helper.rb"
  1. use a full path in the require.  Either

    require 'pathname'

    require Pathname.new(FILE).realpath.dirname.join('/../test_helper.rb')

or

require (File.dirname(File.realdirpath(__FILE__)) + '/../test_helper.rb')

Upvotes: 3

Aetherus
Aetherus

Reputation: 8898

Maybe you should run your test cases in this way:

$ rake test

There is no need to change the "require" statement from generated code if you use rake.

Tested with Ruby 1.9.3 and Rails 3.2.8

Upvotes: 7

Nate Bird
Nate Bird

Reputation: 5335

I've added the following to the top of my test files.

require File.expand_path("../../test_helper", __FILE__)

This restores the previous behavior and allows the call to be simply:

ruby test/unit/person_test.rb

Upvotes: 13

DGM
DGM

Reputation: 26979

ruby 1.9.2 removed ".", the current directory, from the load path. I have to do this to get it to work:

require 'test_helper'

and call it like:

ruby -I. unit/person_test.rb 

Upvotes: 24

Related Questions