设计和password_confirmation验证

Cry*_*ark 2 testing validation ruby-on-rails devise

我在设计和密码确认时遇到了麻烦。我已经在rails4中创建了一个模型测试,如下所示:

test "user requires a password_confirmation" do
  user = FactoryGirl.build(:user)
  assert user.valid?, "FactoryGirl should return a valid user"

  user.password_confirmation = nil

  # These are just some outputs to verify what's going on.
  # This is the condition in devises's password_required? method
  puts !user.persisted? || !user.password.nil? || !user.password_confirmation.nil? #=> true
  puts user.valid? #=> true

  assert user.invalid?, "Password confirmation should be required" # Fails here
  assert_equal 1, user.errors.size, "Only one error should have occured. #{user.errors.full_messages}"
end
Run Code Online (Sandbox Code Playgroud)

该测试在第二个assert("Password confirmation should be required")处失败。

这是我完整的用户模型:

class User < ActiveRecord::Base
  has_one :user_entity

  has_one :manager,through: :user_entity, source: :entity, source_type: 'Manager'
  has_one :client, through: :user_entity, source: :entity, source_type: 'Client'

  # Adds default behavior. See: https://github.com/stanislaw/simple_roles#many-strategy
  simple_roles

  validates :username,
    presence: true,
    length: { minimum: 4, allow_blank: true, if: :username_changed? },
    uniqueness: { allow_blank: true, if: :username_changed? },
    format: { with: /\A[A-z_0-9]+\z/, allow_blank: true, if: :username_changed? }

  # Include default devise modules. Others available are:
  # :token_authenticatable, :timeoutable, :omniauthable, :registerable
  devise  :database_authenticatable, :recoverable, :rememberable,
          :trackable, :validatable, :confirmable, :lockable

  # Virtual attribute for authenticating by either username or email
  # This is in addition to a real persisted field like 'username'
  attr_accessor :login

  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup

    # User need to be active
    conditions[:active] = true

    if login = conditions.delete(:login)
      where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end

  def entity
    self.user_entity.try(:entity)
  end

  def entity=(newEntity)
    self.build_user_entity(entity: newEntity)
  end

end
Run Code Online (Sandbox Code Playgroud)

我尝试添加我的用户模型:

validates :password,    confirmation: true
Run Code Online (Sandbox Code Playgroud)

之后测试通过,但是下一个错误:

  1) Failure:
UserTest#test_user_requires_a_password [/my/project/test/models/user_test.rb:52]:
Two errors should have occured. ["Password confirmation doesn't match confirmation", "Password confirmation doesn't match confirmation", "Password can't be blank"].
Expected: 2
  Actual: 3
Run Code Online (Sandbox Code Playgroud)

如您所见,就像确认确认发生两次,两次都失败一样。

我正在使用rails4(edge)和rails4分支进行设计。

编辑:我尝试设置

user.password_confirmation = "#{user.password}_diff"
Run Code Online (Sandbox Code Playgroud)

测试通过了。那么为什么nil设置时忽略确认?由于该对象尚未保留,因此我认为必须提供密码确认。

J. *_*tte 5

我知道这是一个老问题,但我遇到了同样的问题。

首先,将其从模型中删除,以免重复验证:

validates :password, confirmation: true
Run Code Online (Sandbox Code Playgroud)

然后添加以下内容。我将其更改为仅适用于创建:

validates :password_confirmation, presence: true, on: :create
Run Code Online (Sandbox Code Playgroud)

请参阅此Apidock页面上的validates_confirmation_of部分:

注意:仅当password_confirmation不为nil时,才执行此检查。要要求确认,请确保为确认属性添加状态检查:

validates_presence_of:password_confirmation,如果::password_changed?