![](/img/trans.png)
[英]Michael Hartl's Rails tutorial chapter 12 — undefined method `users'
[英]Michael Hartl's Rails tutorial chapter 12— all 54 tests failed…?
所以我參加了Michael Hartl的Rails教程,第12章,列出了12.7。
我已經寫了一些測試書,但是所有54個測試最終都出現了錯誤。 前幾個是:
ERROR["test_should_require_a_followed_id", RelationshipTest, 0.686335129]
test_should_require_a_followed_id#RelationshipTest (0.69s)
ActiveRecord::RecordNotUnique: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: relationships.follower_id, relationships.followed_id: INSERT INTO "relationships" ("follower_id", "followed_id", "created_at", "updated_at", "id") VALUES (1, 1, '2015-03-10 16:12:17', '2015-03-10 16:12:17', 298486374)
ERROR["test_should_be_valid", RelationshipTest, 0.835273632]
test_should_be_valid#RelationshipTest (0.84s)
ActiveRecord::RecordNotUnique: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: relationships.follower_id, relationships.followed_id: INSERT INTO "relationships" ("follower_id", "followed_id", "created_at", "updated_at", "id") VALUES (1, 1, '2015-03-10 16:12:17', '2015-03-10 16:12:17', 298486374)
ERROR["test_should_require_a_follower_id", RelationshipTest, 0.988648702]
test_should_require_a_follower_id#RelationshipTest (0.99s)
ActiveRecord::RecordNotUnique: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: relationships.follower_id, relationships.followed_id: INSERT INTO "relationships" ("follower_id", "followed_id", "created_at", "updated_at", "id") VALUES (1, 1, '2015-03-10 16:12:18', '2015-03-10 16:12:18', 298486374)
ERROR["test_password_resets", PasswordResetsTest, 1.071410052]
test_password_resets#PasswordResetsTest (1.07s)
ActiveRecord::RecordNotUnique: ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: relationships.follower_id, relationships.followed_id: INSERT INTO "relationships" ("follower_id", "followed_id", "created_at", "updated_at", "id") VALUES (1, 1, '2015-03-10 16:12:18', '2015-03-10 16:12:18', 298486374)
繼續......
這是我的relationship_test.rb:
require 'test_helper'
class RelationshipTest < ActiveSupport::TestCase
def setup
@relationship = Relationship.new(follower_id: 1, followed_id: 2)
end
test "should be valid" do
assert @relationship.valid?
end
test "should require a follower_id" do
@relationship.follower_id = nil
assert_not @relationship.valid?
end
test "should require a followed_id" do
@relationship.followed_id = nil
assert_not @relationship.valid?
end
end
我懷疑在relationship_test.rb中可能有問題,但我甚至不知道從哪里開始所有這些錯誤。 有人能指出我正確的方向嗎? 謝謝。
我已經解決了。
我的relationships.yml
有:
one:
follower_id: 1
followed_id: 1
two:
follower_id: 1
followed_id: 1
這導致測試失敗。 我已將它們評論出來,並且有0個錯誤,但有一個失敗。
根據邁克爾的教程
Listing 12.6: Removing the contents of the relationship fixture.
test/fixtures/relationships.yml
# empty
你必須刪除test/fixtures/relationships.yml
所有內容
並且測試將通過:)
原因是測試將檢查關系的唯一性,我們只是在fixture文件中創建兩個相同的關系。
您的測試數據庫似乎在上次測試后沒有被截斷,並且您的關系表中已經有一條記錄集中的記錄,其中follower_id = 1且follow_id = 1。
創建索引
add_index :relationships, [:follower_id, :followed_id], unique: true
在您的關系中,遷移意味着每個follower_id / follows_id組合都是唯一的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.