【发布时间】:2014-10-19 03:43:36
【问题描述】:
我正在为我的用户模型编写简单的模型测试。其中之一检查:email 属性的存在:
it "is invalid without an email" do
expect(user = User.new(email: nil)).to have(1).errors_on(:email)
end
由于某种原因,此测试失败,因为错误哈希包含两次“电子邮件不能为空白”:
Failures:
1) User is invalid without an email
Failure/Error: expect(user = User.new(email: nil)).to have(1).errors_on(:email)
expected 1 errors on :email, got 2
当我在控制台中使用nil 电子邮件创建用户并调用user.errors.full_messages 时,我得到以下信息:
@messages=
{:email=>["can't be blank", "can't be blank"]
这就是我的验证在user.rb 中的样子:
validates_presence_of :email
奇怪的是,如果我从user.rb 中删除上面的代码,测试现在通过了,我只会在错误哈希中得到一次错误。
这里发生了什么?我应该注意到我的用户模型是通过设计生成器创建的。 Devise 是否已经包含对:email 的验证??
【问题讨论】:
标签: ruby-on-rails ruby testing rspec devise