2015-09-04 25 views
1

我正在使用rails 4.2.1 & ruby​​ 2.2.1在我的應用程序中。所以minitest會自動添加,版本號爲5.1。我的應用程序中沒有數據庫。 有了數據庫,我可以測試模型。如何在沒有數據庫的情況下測試模型?如何用Minitest和沒有數據庫測試模型?

我創建了一個用戶模型:

class User 
    include ActiveModel::Model 
end 

users.yml裏:

one: 
    firstName: Avi 
    email: [email protected] 

user_test.rb:

require 'test_helper' 

class UserTest < ActiveSupport::TestCase  
    test "the truth" do 
    user = users(:one) 
    assert true 
    end 
end 

在這裏,我得到的錯誤:Undefined methods users。如果daabase存在,我會獲得正確的數據。 我甚至嘗試加入include ActiveModel::Lint::Tests仍然收到相同的錯誤。 任何人都可以幫助我嗎?

由於

回答

0

ActiveSupport::TestCase期望數據庫連接是激活的。您可能想將其切換到Minitest::Test,但這意味着您不能使用夾具方法從數據庫中檢索記錄。

require 'test_helper' 

class UserTest < Minitest::Test 
    def test_sanity 
    user = User.new 
    user.first_name = "Avi" 
    user.email = "[email protected]" 
    assert user 
    end 
end 
+0

稍微有點棘手,我找到了。您需要手動導入minitest,而不是* test_helper.rb *中的默認* rails/test_help * ...當我將它分類後,會報告更多... – ericpeters0n