【发布时间】:2014-10-24 14:39:37
【问题描述】:
我想测试用户模型的唯一性。
我的User 模型类看起来像:
class User
include Mongoid::Document
field :email, type: String
embeds_one :details
validates :email,
presence: true,
uniqueness: true,
format: {
with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z0-9]{2,})\Z/i,
on: :create
},
length: { in: 6..50 }
end
我属于模型的 rspec 测试如下:
...
before(:each) do
FactoryGirl.create(:user, email: taken_mail)
end
it "with an already used email" do
expect(FactoryGirl.create(:user, email: taken_mail)).to_not be_valid
end
在我执行bundle exec rspec 之后,它总是引发下一个错误,而不是成功通过:
Failure/Error: expect(FactoryGirl.create(:user, email: taken_mail)).to_not be_valid
Mongoid::Errors::Validations:
Problem:
Validation of User failed.
Summary:
The following errors were found: Email is already taken
Resolution:
Try persisting the document with valid data or remove the validations.
如果我使用它,它会成功通过:
it { should validate_uniqueness_of(:email) }
我想使用expect(...)。谁能帮帮我?
【问题讨论】:
-
如果您尝试:
expect(FactoryGirl.build(:user, email: taken_mail)).to_not be_valid会怎样?使用构建而不是创建 -
它工作正常并且测试通过但我不知道为什么当我使用
create方法时会出现错误。如果我想使用FactoryGirl.create(...)怎么办?如果您将其留在答案中,我会给您重点。
标签: ruby-on-rails validation rspec mongoid validates-uniqueness-of