Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Changed the "require test_helper" to "require File.dirname(FILE) + "/../test_helper" " in test/unit/mytest_test.rb. It brings back " no such file to load -- test/unit/../test_helper"
  • Tried running rvm test/unit/mytest_test.rb Same as above
  • Tried running ruby -I test/unit/mytest_test.rb. No messages to the terminal. After about 5 minutes waiting for something to happen, ctrl+c'd out of it

Any suggestions very appreciated - I'm stumped.

like image 603
PlankTon Avatar asked Oct 16 '10 20:10

PlankTon


People also ask

How to test controllers in Rails?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

How to run test cases in Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

How do you test a method in Ruby?

Most methods can be tested by saying, “When I pass in argument X, I expect return value Y.” This one isn't so straightforward though. This is more like “When the user sees output X and then enters value V, expect subsequent output O.” Instead of accepting arguments, this method gets its value from user input.


2 Answers

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 
like image 156
DGM Avatar answered Oct 17 '22 11:10

DGM


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

like image 15
Redrick Avatar answered Oct 17 '22 10:10

Redrick