nyphur
nyphur

Reputation: 2866

Michael Hartl's Rails tutorial chapter 12-- all 54 tests failed...?

So I'm at Michael Hartl's Rails Tutorial, chapter 12, listing 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:

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:

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.

Upvotes: 4

Views: 993

Answers (3)

davideghz
davideghz

Reputation: 3685

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.

Upvotes: 1

nyphur
nyphur

Reputation: 2866

I've solved it.

My relationships.yml had:

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.

Upvotes: 7

Klaus
Klaus

Reputation: 1771

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.

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.

Upvotes: 0

Related Questions