将索引添加到电子邮件列导致单元测试失败的罪魁祸首

发布于 2024-11-02 23:18:36 字数 517 浏览 3 评论 0原文

我是设计和轨道的新手,我刚刚将设计与我的示例应用程序集成。我注意到创建用户模型后,我的所有单元测试都失败了。我尝试缩小范围,发现即使为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-09 23:18:36

如果您刚刚生成了设计模型,也会生成一个包含或多或少以下内容的固定装置:

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

该固定装置尝试创建两个用户,并使用相同的(不存在的)电子邮件。将其替换为:

one:
  email: [email protected]

two:
  email: [email protected]

它将修复此错误。

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: [email protected]

two:
  email: [email protected]

It will fix this error.

傲世九天 2024-11-09 23:18:36

故障必须发生在实际测试之前的测试设置中。您可能正在尝试通过固定装置或工厂来设置具有相同电子邮件地址的用户。

如果您使用固定装置,请确保为每个用户提供不同的电子邮件地址。如果您使用工厂,请使用序列为创建的每个用户生成唯一的电子邮件地址。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文