2014-11-24 59 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

相關問題