我按照Michael Hartl 的 The Ruby on Rails Tutorial Book 进行了修改,添加了用于用户身份验证的设计,并遇到了电子邮件唯一性测试的问题。
# spec/models/user_spec.rb
require 'spec_helper'
describe User do
before do
@user = User.new(username: 'ExampleUser',
email: 'user@example.com',
password: 'passworD123')
@user.save
end
subject { @user }
it { should respond_to(:email) }
it { should respond_to(:name) }
it { should respond_to(:username) }
it { should respond_to(:password) }
it { should be_valid }
describe 'username is already taken' do
before do
user_with_same_username = @user.dup
user_with_same_username.username = @user.username.upcase
user_with_same_username.email = 'a@b.c'
user_with_same_username.save
end
it { should_not be_valid }
end
describe 'email address is already taken' do
before do
user_with_same_email = @user.dup
user_with_same_email.username = 'differentUsername'
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
.
.
失败:
1) User email address is already taken
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/user_spec.rb:104:in `block (3 levels) in <top (required)>'
电子邮件的唯一性已设置,尽管不需要,因为 Devise 已经这样做了。我更改user_with_same_email.save
为user_with_same_email.save!
,然后我收到了电子邮件地址的验证错误,因为它已经被占用:
1) User email address is already taken
Failure/Error: user_with_same_email.save!
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/models/user_spec.rb:101:in `block (3 levels) in <top (required)>'
的返回值为user_with_same_email.save
false 并it { should_not be_valid }
检查这种情况,但为什么测试仍然失败?