2010-10-16 61 views
25

我试图通过ruby test/unit/mytest.rb运行单个测试,但我总是得到一个“no such file to load - test_helper”错误。谷歌提出了一些建议,但他们都没有为我工作。我跑的Rails 3.0的Ruby 1.9.2(通过RVM)在Ubuntu 10.10Rails tests can not find test_helper

这是我到目前为止已经试过 - 任何建议,非常感谢

  • 改变了“需要test_helper”到“需要File.dirname(FILE)+ test/unit/mytest_test.rb中的“/../test_helper” “。它带回“没有这样的文件来加载 - 测试/单元/../ test_helper”
  • 试图运行RVM测试/单元/ mytest_test.rb同上
  • 试图运行红宝石-I试验/单元/ mytest_test。 RB。没有消息到终端。大约5分钟后,等待发生的事情,按Ctrl + c'd它

任何建议非常赞赏 - 我很难过。

回答

23

ruby​​ 1.9.2从加载路径中删除“。”,即当前目录。我这样做是为了得到它的工作:

require 'test_helper' 

,并调用它像:

ruby -I. unit/person_test.rb 
+2

胆碱!太棒了 - 非常感谢。 – PlankTon 2010-10-17 04:13:39

+4

ruby​​ -Itest test/unit/person_test.rb。参考:http://guides.rubyonrails.org/testing.html#running-tests – 2013-01-22 09:25:38

+0

唯一的区别是你在什么目前的目录... – DGM 2013-01-22 15:08:09

12

我添加以下到我的测试文件的顶部。

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

这将恢复以前的行为,并允许呼叫是简单:

$ rake test 

没有必要:

ruby test/unit/person_test.rb 
6

也许你应该以这种方式运行测试用例如果您使用rake,请更改生成的代码中的“require”语句。

测试用Ruby 1.9.3和Rails 3.2.8

3

的Rails 1.9不再包括在LOAD_PATH,这会导致该问题的当前目录。你有几个选择。

  1. 呼叫与来自该应用DIR -I选项测试:

    红宝石-I测试测试/功能/ test_foo.rb

,并使用不带路径的需要:

require "test_helper.rb" 
  1. 用在需要一个完整的路径。任一

    需要 '路径'

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

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

如果要创建的宝石或引擎,在测试假人的应用程序目录中运行rake test将导致此错误。在宝石的根部运行rake test将避免这种情况。

11

我今天所以现在它看起来像打架这件事我和我不喜欢用全路径的大需要到文件之类的东西......

对我来说,这是Rake文件的故障..

这样的:

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 

我知道它老了答案标接受,但也许这也将帮助别人:) 有一个愉快的一天