Michael Hartl 的 Rails 教程第 12 章——所有 54 个测试都失败了...?
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 中可能有问题,但我什至不知道从哪里开始所有这些错误。有人能指出我正确的方向吗?谢谢。
您的测试数据库似乎在您上次测试后没有被截断,并且您已经在您的关系 table 中有一个记录集,其中 follower_id = 1 和 followed_id = 1.
正在创建索引
add_index :relationships, [:follower_id, :followed_id], unique: true
在您的关系迁移中,每个 follower_id/followed_id 组合都必须是唯一的。
我已经解决了
我的 relationships.yml
有:
one:
follower_id: 1
followed_id: 1
two:
follower_id: 1
followed_id: 1
导致测试失败。我已经把它们注释掉了,有 0 个错误,但 1 个失败。
根据迈克尔的教程
Listing 12.6: Removing the contents of the relationship fixture.
test/fixtures/relationships.yml
# empty
您必须删除 test/fixtures/relationships.yml
的所有内容
测试将通过:)
原因是测试会检查关系的唯一性,而我们只是在夹具文件中创建两个相同的关系。
所以我在看 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 中可能有问题,但我什至不知道从哪里开始所有这些错误。有人能指出我正确的方向吗?谢谢。
您的测试数据库似乎在您上次测试后没有被截断,并且您已经在您的关系 table 中有一个记录集,其中 follower_id = 1 和 followed_id = 1.
正在创建索引
add_index :relationships, [:follower_id, :followed_id], unique: true
在您的关系迁移中,每个 follower_id/followed_id 组合都必须是唯一的。
我已经解决了
我的 relationships.yml
有:
one:
follower_id: 1
followed_id: 1
two:
follower_id: 1
followed_id: 1
导致测试失败。我已经把它们注释掉了,有 0 个错误,但 1 个失败。
根据迈克尔的教程
Listing 12.6: Removing the contents of the relationship fixture.
test/fixtures/relationships.yml
# empty
您必须删除 test/fixtures/relationships.yml
测试将通过:)
原因是测试会检查关系的唯一性,而我们只是在夹具文件中创建两个相同的关系。