我有一个名为Candidate的Devise模型。
我得到了这个测试:
it { expect(subject).to validate_presence_of(:democracyengine_recipient_id) }
模型中的这一行:
validates :democracyengine_recipient_id, presence: true
验证工作(在其他测试中)。
但是在这个特定的测试中我得到了这个错误:
Failure/Error: it { expect(subject).to validate_presence_of(:democracyengine_recipient_id) }
Expected errors to include "can't be blank" when democracyengine_recipient_id is set to nil, got errors: ["email can't be blank (\"\")", "password can't be blank (nil)"]
我该如何解决?
答案 0 :(得分:1)
presence: true
是一个ActiveRecord验证器,在我看来它应该被调用如下:(注意:验证并且不验证)
validates :democracyengine_recipient_id, presence: true
validate
方法向类添加验证方法或块。当覆盖验证实例方法变得过于笨拙并且您正在寻找更具描述性的验证声明时,这非常有用。
请参阅API Documentation for validate
对于validates
,请参阅官方API Documentation for validates