2014-11-24 58 views
1

当我试图运行下面的代码:未初始化的常量的ActiveSupport :: TestCase的

测试/模型/ tweet_test.rb

require 'minitest/spec' 
require 'minitest/autorun' 

class TweetTest < ActiveSupport::TestCase 
    test "Tweet should not be null" do 
    tweet = true 
    assert tweet 
    end 
end 

我收到此错误:

tweet_test.rb:4:in `<main>': uninitialized constant ActiveSupport (NameError) 

我正在跟着一个完美的教程。这是为什么发生? ActiveSuport :: TestCase已被弃用?

更新: 我试图require 'test_helper'

require 'minitest/spec' 
require 'minitest/autorun' 
require 'test_helper' 

class TweetTest < ActiveSupport::TestCase 
    test "Tweet should not be null" do 
    tweet = true 
    assert tweet 
    end 
end 

但收到以下错误:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test_helper (LoadError) 
+1

如果*只*需要'test_helper'会怎么样?并且是你的测试文件在'/ test'文件夹内? – zwippie 2014-11-24 20:14:06

+0

同样的事情。是的,这是在test/models/tweet_test.rb里面 – 2014-11-24 20:54:10

+1

是否存在文件'/ test/test_helper.rb'并且它包含了一些看起来像在运行'rails new my_project'之后从未搞混过的东西?如果没有,则创建一个新的空Rails项目,并将新生成的'test_helper'文件复制到当前项目中。 – zwippie 2014-11-24 20:59:53

回答

2

你是不是需要正确的Rails MINITEST文件设置一个ActiveSupport::TestCase。这是一个Rails特定的类,无法在minitest中找到。你确定你在Rails项目中工作吗?

在大多数情况下,您通过在测试类中要求test_helper在Rails中设置测试,以便您可以从ActiveSupport::TestCase继承。文件test_helper包含在Rails项目中运行测试的所有要求和设置。

快速修复:刚刚从Minitest::Test继承。

+0

是的,我从Rails内部运行。我试图测试我的一个模型。要求test_helper也不起作用。我会发布上述结果 – 2014-11-24 20:04:50

相关问题