2017-12-18 420 views
2

我有問題,我在軌 我從railstutorial.org學習項目,我在上市的有問題〜6.18 所以這是回購 https://github.com/Mroczeks/first_project 這是錯誤:沒有ID哈特爾找不到用戶軌教程

rails test 
Running via Spring preloader in process 10776 
Started with run options --seed 33787 

FAIL["test_schould_be_valid", UserTest, 0.6801217989996076] 
test_schould_be_valid#UserTest (0.68s) 
     Expected false to be truthy. 
     test/models/user_test.rb:9:in `block in <class:UserTest>' 

ERROR["test_email_addresses_should_be_saved_as_lower-case", UserTest, 0.6879979369996363] 
test_email_addresses_should_be_saved_as_lower-case#UserTest (0.69s) 
ActiveRecord::RecordNotFound:   ActiveRecord::RecordNotFound: Couldn't find User without an ID 
      test/models/user_test.rb:45:in `block in <class:UserTest>' 

FAIL["test_email_validation_should_accept_valid_addresses", UserTest, 0.7056386839994957] 
test_email_validation_should_accept_valid_addresses#UserTest (0.71s) 
     "[email protected]" should be valid 
     test/models/user_test.rb:32:in `block (2 levels) in <class:UserTest>' 
     test/models/user_test.rb:30:in `each' 
     test/models/user_test.rb:30:in `block in <class:UserTest>' 

    18/18: [=================================] 100% Time: 00:00:00, Time: 00:00:00 

Finished in 0.78367s 
18 tests, 29 assertions, 2 failures, 1 errors, 0 skips 
+0

「test/models/user_test.rb」文件中有什麼? –

+0

模型用戶測試(名稱,電子郵件和通過bcrypt)所以我測試例如密碼是空的或具有指定數量的字符。 – Drekoo

+0

https://github.com/Mroczeks/first_project/blob/master/test/models/user_test.rb – Drekoo

回答

3

此錯誤的原因似乎user_test.rb文件保存@user對象時要失敗驗證。試着用下面的代碼替換這個測試並運行這個測試時,你會看到驗證失敗:

test "email addresses should be saved as lower-case" do 
    mixed_case_email = "[email protected]" 
    @user.email = mixed_case_email 

    # Calling `save!` here will raise an exception if failed to save @user. 
    @user.save! 
    assert_equal mixed_case_email.downcase, @user.reload.email 
end 

解決您的setup方法相應地滿足所有驗證,再嘗試。

3

User模式,你必須defined password length validation爲6作爲最小的,但在測試設置,created the userpassword: 'pies'。因此,你讓所有的錯誤和失敗:

FAIL["test_schould_be_valid", UserTest, 0.6801217989996076] 
test_schould_be_valid#UserTest (0.68s) 
    Expected false to be truthy. 
    test/models/user_test.rb:9:in `block in <class:UserTest>' 

要解決所有的錯誤,你需要在UserTest#setup初始化有效數據的用戶。在這種情況下,您只需提供一個最小長度爲6的密碼。

相關問題