将索引添加到电子邮件列导致单元测试失败的罪魁祸首
我是设计和轨道的新手,我刚刚将设计与我的示例应用程序集成。我注意到创建用户模型后,我的所有单元测试都失败了。我尝试缩小范围,发现即使为 user_test.rb
生成的断言“真相”也失败了:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: \
column email is not unique: INSERT INTO "users" (...
一旦我注释掉了 add_index
...
# add_index :users, :email, :unique => true
。 ..并重新运行rake db:test:load
并使用ruby -I test test/unit/user_test.rb
重新运行测试,它通过了。
还有其他人经历过吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您刚刚生成了设计模型,也会生成一个包含或多或少以下内容的固定装置:
该固定装置尝试创建两个用户,并使用相同的(不存在的)电子邮件。将其替换为:
它将修复此错误。
If you just generated the devise model, a fixture was also generated with more or less this content:
This fixture tries to create two users, with the same (inexistant) email. Replace it by:
It will fix this error.
故障必须发生在实际测试之前的测试设置中。您可能正在尝试通过固定装置或工厂来设置具有相同电子邮件地址的用户。
如果您使用固定装置,请确保为每个用户提供不同的电子邮件地址。如果您使用工厂,请使用序列为创建的每个用户生成唯一的电子邮件地址。
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.