So I'm at Michael Hartl's Rails Tutorial, chapter 12, listing 12.7.
所以我在Michael Hartl的Rails教程中,第12章,清单12.7。
I've written a few tests as followed by the book, but all 54 tests ended up with errors. The first few are:
我已经写了一些测试,如书中所示,但是所有的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)
and continues...
并继续……
Here is my relationship_test.rb:
这是我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
I have a suspicion that something may be wrong in relationship_test.rb, but I don't even know where to start with all these errors. Could someone point me in the right direction? Thank you.
我怀疑在关系测试中可能出了问题。rb,但是我甚至不知道从哪里开始这些错误。有人能告诉我正确的方向吗?谢谢你!
3 个解决方案
#1
5
I've solved it.
我已经解决了它。
My relationships.yml
had:
我的人际关系。yml:
one:
follower_id: 1
followed_id: 1
two:
follower_id: 1
followed_id: 1
which was causing the tests to fail. I've commented them out and there were 0 errors, but one failure.
这导致测试失败。我已经注释了它们,有0个错误,但只有一个失败。
#2
1
as per michael's tutorial
按照迈克尔的教程
Listing 12.6: Removing the contents of the relationship fixture.
test/fixtures/relationships.yml
# empty
you have to delete all content of test/fixtures/relationships.yml
你必须删除测试/装置/关系的所有内容
and the test will pass :)
测试通过:)
The reason is that test will check for uniqueness of relationships, and we just create two identical relationships in the fixture file.
原因是测试将检查关系的唯一性,我们只是在fixture文件中创建两个相同的关系。
#3
0
It seems that your test database wasn't truncated after your last test and you already have a record in your relationships table with a recordset where follower_id = 1 and followed_id = 1.
看起来您的测试数据库在上次测试之后并没有被截断,并且您的关系表中已经有一个记录,其中的follower_id = 1和followed_id = 1。
Creating an index
创建索引
add_index :relationships, [:follower_id, :followed_id], unique: true
in your relationships migration means, that each follower_id/followed_id combination msut be unique.
在关系迁移中,每个follower_id/followed_id组合msut都是惟一的。
#1
5
I've solved it.
我已经解决了它。
My relationships.yml
had:
我的人际关系。yml:
one:
follower_id: 1
followed_id: 1
two:
follower_id: 1
followed_id: 1
which was causing the tests to fail. I've commented them out and there were 0 errors, but one failure.
这导致测试失败。我已经注释了它们,有0个错误,但只有一个失败。
#2
1
as per michael's tutorial
按照迈克尔的教程
Listing 12.6: Removing the contents of the relationship fixture.
test/fixtures/relationships.yml
# empty
you have to delete all content of test/fixtures/relationships.yml
你必须删除测试/装置/关系的所有内容
and the test will pass :)
测试通过:)
The reason is that test will check for uniqueness of relationships, and we just create two identical relationships in the fixture file.
原因是测试将检查关系的唯一性,我们只是在fixture文件中创建两个相同的关系。
#3
0
It seems that your test database wasn't truncated after your last test and you already have a record in your relationships table with a recordset where follower_id = 1 and followed_id = 1.
看起来您的测试数据库在上次测试之后并没有被截断,并且您的关系表中已经有一个记录,其中的follower_id = 1和followed_id = 1。
Creating an index
创建索引
add_index :relationships, [:follower_id, :followed_id], unique: true
in your relationships migration means, that each follower_id/followed_id combination msut be unique.
在关系迁移中,每个follower_id/followed_id组合msut都是惟一的。