Rspec引发错误:
1) Client uniqueness validates uniqueness of email
Failure/Error: expect(subject).to validate_uniqueness_of :email
Client did not properly validate that :email is case-sensitively unique.
After taking the given Client, whose :email is
‹"jaleel_wehner@okonwiegand.name"›, and saving it as the existing
record, then making a new Client and setting its :email to a different
value, ‹"JALEEL_WEHNER@OKONWIEGAND.NAME"›, the matcher expected the
new Client to be valid, but it was invalid instead, producing these
validation errors:
* pesel: ["This pesel is already in database"]
* email: ["This email is already in database"]
在模型中,我实现了唯一性和区分大小写:错误的电子邮件。
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX }
我还实现了一种方法,即在验证之前将所有电子邮件发送到邮箱中。
def downcase_email
self.email = email.downcase if email.present?
end
before_validation :downcase_email
为什么匹配器期望新客户端有效?它应该是无效的。
subject { FactoryGirl.build(:client) }
it 'validates uniqueness of email' do
expect(subject).to validate_uniqueness_of :email
end
客户有一个有效的工厂。我试过找到好的解决方案,但我找不到任何可以解决我问题的方法。
FactoryGirl.define do
factory :client do
pesel { Faker::Number.number(11) }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
date_of_birth { Faker::Time.between('1970-01-01', '2000-12-31') }
email { Faker::Internet.email }
password { Faker::Internet.password }
type 'Client'
end
end
答案 0 :(得分:2)
FactoryGirl在其中提供了以下示例文档
sequence :email do |n|
"person#{n}@example.com"
end
factory :invite do
invitee { generate(:email) }
end
更新后修改:
问题是匹配器validate_uniqueness_of
。您还必须为匹配器调整case_sensitive
。所以它应该是validate_uniqueness_of(:email).case_insensitive
答案 1 :(得分:0)
检查您的工厂是否有包含电子邮件属性的弯曲括号,如下所示:
FactoryGirl.define do
factory :client do
email { Faker::Internet.email }
end
end