【发布时间】:2013-01-20 11:37:33
【问题描述】:
这是失败的测试:
context "when password does not match confirmation" do
before { build(:user, :password_confirmation => 'mismatch') }
it { should_not be_valid }
end
我在这个测试套件中使用 Factory Girl 的 build 方法。
我的用户模型:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
validates :password_confirmation, presence: true
validates :password, presence: true, length: { minimum: 5 }
end
更新:在使用 has_secure_password 时,您不需要使用 :confirmation => true 标记 :password 字段。处理好了:见源码:https://github.com/rails/rails/blob/master/activemodel/lib/active_model/secure_password.rb
验证工作正常 - 当我尝试在控制台创建新用户时收到正确的错误消息。那么为什么测试失败了呢?错误在哪里?
【问题讨论】:
标签: ruby-on-rails-3 authentication rspec