2013-03-27 3 views
1

기기 및 비밀번호 확인에 문제가 있습니다. I는 다음과 같습니다 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 

이 시험은 두 번째 어설 ("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 

내 사용자 모델에 추가하려고했습니다 :

여기 내 전체 사용자 모델의 그 테스트 통과 후

validates :password, confirmation: true 

하지만 난 다음 하나에 오류가 발생합니다 :

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 

확인 된 것처럼 확인은 2 번 발생하며 두 가지 경우 모두 실패합니다.

저는 rails4 (가장자리)와 rails4 분기를 사용하고 있습니다.

편집 : 나는

user.password_confirmation = "#{user.password}_diff" 

설정을 시도하고 테스트가 통과한다. 그렇다면 nil이 설정되면 확인이 무시되는 이유는 무엇입니까? 객체가 아직 저장되지 않았기 때문에 암호 확인이 제공되어야한다고 가정합니다.

답변

1

이 질문은 오래된 질문이지만 동일한 문제가있는 것으로 알고 있습니다. 중복 유효성 검사를하지 않도록

먼저, 모델이 제거 :

validates :password, confirmation: true 

는 다음을 추가합니다. 내가에만 작업에 변경 작성 :

참고 :

validates :password_confirmation, presence: true, on: :create 

this apidock page 섹션 validates_confirmation_of를 참조하십시오이 검사는 password_confirmation가 nil이 아닌 경우에만 수행됩니다. 확인을 요구하려면 확인 속성에 대한 존재 확인을 추가하십시오.

validates_presence_of : 해당되는 경우 : password_changed?