应允许空白 validate_uniqueness_of
使用应该,任何想法如何在进行此测试时允许空白:
should validate_uniqueness_of(:email)
谢谢。
Using shoulda, any ideas how to allow blank when having this test:
should validate_uniqueness_of(:email)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经想通了!! validate_uniqueness_of 依赖于您当前在数据库中的第一条记录上测试的属性的值。因此,如果数据库中的第一条记录恰好为空白或该属性的 nul,您的测试将始终给出如下错误:“失败/错误:it { should validate_uniqueness_of(:customer_number).case_insensitive.scoped_to(:user_id) }当 customer_number 设置为 nil 时,预期错误包括“已被采取”,没有错误”。
那么我们该如何解决这个问题呢?确保删除所有现有记录,并且第一条记录包含填充的属性值。
型号:
型号规格:
希望这可以解决一些人的问题:)
I have figured it out!! The validate_uniqueness_of relies on the value for the attribute you are currently testing on the first record in the database. So, if the first record in the database happens to be blank or nul for that attribute your test will always give an error like this: "Failure/Error: it { should validate_uniqueness_of(:customer_number).case_insensitive.scoped_to(:user_id) } Expected errors to include "has already been taken" when customer_number is set to nil, got no errors".
So how do we fix this? Make sure you delete all existing records and that the first record contains a filled in value for the attribute.
model:
model spec:
Hope this cleared up some people's issues :)
或许
maybe