我正在尝试编写一个验证电子邮件地址唯一性的测试。
这是用于在Rails 4.0.0中创建新用户帐户并使用has_secure_password。
我收到此错误:
User should require case sensitive unique value for email
Failure/Error: it { should validate_uniqueness_of(:email) }
RuntimeError:
Password digest missing on new record
# ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
user_spec.rb
:
require "spec_helper"
describe User do
it { should validate_presence_of(:email) }
it { should validate_presence_of(:full_name) }
it { should validate_presence_of(:password_digest) }
it { should validate_uniqueness_of(:email) }
end
以下是用户模型:
class User < ActiveRecord::Base
has_secure_password
validates :email, presence: true, uniqueness: true
validates :full_name, presence: true
validates :password_digest, presence: true
end
错误消息的含义是什么?我做错了什么?
答案 0 :(得分:1)
问题解决了。我在每次测试之前添加了一行来创建一个User对象,现在错误就消失了。
describe User do
# this before section solved the problem
before(:each) do
User.create(
full_name: "Bob",
email: "bob@bob.com",
password: "bob",
password_confirmation: "bob"
)
end
it { should validate_presence_of(:email) }
it { should validate_presence_of(:full_name) }
it { should validate_presence_of(:password_digest) }
it { should validate_uniqueness_of(:email) }
end
答案 1 :(得分:0)
回答你的问题:https://github.com/thoughtbot/shoulda-matchers/issues/371
似乎当前的解决方案是在测试唯一性之前创建新记录。简而言之,这是潜在的问题:
似乎
has_secure_password
的Rails 4版本增加了一个 before_create以确保填写password_digest
;然而, Rails 3版本没有这个检查。