2013-10-01 44 views
1

我試圖在新的Rails 4安裝中使用Minitest。我的理解是,如果我不從ActiveRecord的繼承類,那麼我應該能夠使用MINITEST本身不帶滑軌的整合:Minitest - Rails 4中的測試類

#test/models/blog.rb 
require "minitest/autorun" 
class Blog < Minitest::Unit::TestCase 

def setup 
    @b = Blog.new 
end 

def test_entries 
    assert_empty "message", @b.entries 
end 

#app/models/blog.rb 
class Blog 
    attr_reader :entries 
    def initialize 
    @entries = [] 
    end 

我運行ruby test/models/blog.rb測試。 我的問題與設置方法。如果我沒有爲我的博客添加條目,則測試將失敗,並顯示在安裝中存在錯誤數量的參數。如果我在設置信息@b = Blog.new entries: "Twilight"中包含條目,則我的測試在test_entries方法中失敗,因爲條目是未定義的方法。

回答

3

你有幾個問題。首先,你不需要「test_helper」,這意味着當你運行這個測試時rails沒有被加載,這意味着沒有加載rails用於解析缺少的常量的機制。您需要直接要求幫助者或需要博客文件。其次,你用測試覆蓋了你想測試的常量,這就是爲什麼你會得到令人困惑的消息。請將測試類BlogTest命名爲避免此情況。

這是我認爲你正在嘗試做的:

require "minitest/autorun" 
require "models/blog" # Assuming "app" is in your load path when running the test 
#require "test_helper" # Or require this instead if you need to use DB 

class BlogTest < Minitest::Unit::TestCase 

    def setup 
    @b = Blog.new 
    end 

    def test_entries 
    assert_empty @b.entries, "Blog entries should be empty" 
    end 

end 
+0

問題解決了。我不知道如何將「app」添加到我的加載路徑,所以我使用了'require_relative'../../ app/models/blog''。我遵循Avdi的ObjectsOnRails,它開始時避免了任何Rails集成來設置和運行測試,這就是爲什麼我現在要避免要求「test_helper」並且還要運行'rake test'。 – Marklar