Rails - 所有测试都失败 - '... ConstraintException:UNIQUE约束失败:admins.email ...'

时间:2017-02-20 16:53:26

标签: ruby-on-rails

当我运行这个:rails test时,我的所有(10)测试都会失败,并出现相同的错误(唯一约束失败)。这是错误消息:

E

Error:
WelcomeControllerTest#test_the_truth:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE 
constraint failed: admins.email: INSERT INTO "admins" ("created_at", 
"updated_at", "id") VALUES ('2017-02-20 16:22:33.516784', '2017-02-20  
16:22:33.516784', 298486374)

bin/rails test test/controllers/welcome_controller_test.rb:4

其中一项测试是:

test "the truth" do
  assert true
end

通过浏览,我看到它可能是关于灯具的东西。在/test/fixtures/admin.yml然后就在那里:

# This model initially had no columns defined. If you 
# add columns to the model remove the '{}' from the fixture 
# names and add the columns immediately below each fixture, 
# per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

如果我然后注释掉one: {}two: {},那么它就可以了。我不知道为什么会这样?请问有人解释一下吗?

1 个答案:

答案 0 :(得分:2)

它不喜欢你使用两个nil的电子邮件,它期望每个模型的唯一电子邮件地址。将其更改为:

# This model initially had no columns defined. If you 
# add columns to the model remove the '{}' from the fixture 
# names and add the columns immediately below each fixture, 
# per the syntax in the comments below
#
one: 
  email: 'one@one.com'

two:
  email: 'two@two.com'