adding index to email column culprit for unit test

2020-02-23 06:26发布

I am new to devise and rails and I just integrated devise with my sample app. What I noticed is after creating a user model, all my unit tests were failing. I went to try to narrow this and found that even the generated assert 'the truth' one for user_test.rb also fails:

ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: \
    column email is not unique: INSERT INTO "users" (...

Once I commented out the add_index ...

# add_index :users, :email, :unique => true

... and re-ran rake db:test:load and re-run tests with ruby -I test test/unit/user_test.rb it passes.

Has anyone else experience this?

2条回答
▲ chillily
2楼-- · 2020-02-23 06:34

If you just generated the devise model, a fixture was also generated with more or less this content:

one: {}
# column: value
#
two: {}
#  column: value

This fixture tries to create two users, with the same (inexistant) email. Replace it by:

one:
  email: test1@test.com

two:
  email: test2@test.com

It will fix this error.

查看更多
Deceive 欺骗
3楼-- · 2020-02-23 06:34

The failure would have to be happening in the test setup, before the actual test. You are probably trying to set up users with identical email addresses, either via fixtures or factories.

If you are using fixtures, make sure each user is given a different email address. If you are using factories, use a sequence to generate a unique email addresses for each user that's created.

查看更多
登录 后发表回答