Lethjakman
Lethjakman

Reputation: 997

Rails 4 testing user initialization is always blank

Hey I have a test that looks like this

test 'create account' do
    if User.create(email: '[email protected]', password: 'blahblah')
        assert true
    else
        assert User.msg
    end
end

But when I try to run it I'm getting an error message like this:

  1) Error:
UserTest#test_create_account:
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_users_on_email"
DETAIL:  Key (email)=() already exists.
: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2013-10-16 21:59:54', '2013-10-16 21:59:54', 298486374)

This looks to me as though I hadn't initialized email, but as I understand this should be initialized with my create above. I'm using strong params so I don't have any attr_accessable enabled and I can run this through. Does anyone know what could be causing this? If you want any more information let me know.

Upvotes: 6

Views: 962

Answers (2)

a learner has no name
a learner has no name

Reputation: 805

If you receive a PostgreSQL unique key violation error message, perhaps your primary key index is out of sync, e.g. after populating the database.

Use ActiveRecord::Base.connection.reset_pk_sequence!('users') to bring the primary key index for the User table in sync again.

Upvotes: 0

Lethjakman
Lethjakman

Reputation: 997

This was caused by the automatically generated fixtures from the rails scaffolding. For whatever reason this shows up inside of the tests rather than in its own section. When I fixed the fixtures this error stopped appearing.

Upvotes: 8

Related Questions