2014-02-23 3 views
2

사용자 로그인을위한 오이 테스트를 작성하고 있습니다.Devise : db에서 사용자 비밀번호를 얻는 방법

나는 그런 시나리오 배경, 즉 추가 한 데이터베이스에 대한 사용자

User.create!(

    :email => email, 

    :password => password, 

    :password_confirmation => password 

    ) 

그래서, 왜 내 순차적 인 단계에서, 내가 @ user.password 전무거야?

@user = User.find_by :email => "[email protected]" 

    ... 

    fill_in('Email', :with => @user.email) 

    fill_in('Password', :with => @user.password) 
    ... 

답변

5

Devise는 처음에 원래의 암호를 암호화하여 저장합니다. encrypted_password (모델의 필드 이름)이 데이터베이스에 저장됩니다.

이제 User.find_by :email => "[email protected]"으로 전화하면 password 필드가 존재하지 않습니다. password은 모델 사용자의 필드가 아닙니다. 이 모델의 인스턴스 변수는 login/sign up 양식을 제출할 때만 설정됩니다. 귀하의 경우, log in 전에 암호 필드에 액세스하려고하므로 인스턴스 변수가 설정되지 않고 password이 nil이됩니다.

당신이 더 많은 정보를 원한다면, 당신은 더 많은 정보에 대한 devise cucumber test 위키 페이지 출력 Devise password

1

검사의 실제 구현을 확인할 수 있습니다.

Kirti는 User 모델의 속성으로 암호에 액세스 할 수 없다는 것을 지적합니다. 특히 일반 텍스트가 아닙니다.

로그인을 테스트하는 경우 암호 필드를 변수로 유지하거나 해당 테스트에서 사용자를 만듭니다.

Given /^I am a new, authenticated user$/ do 
    email = '[email protected]' 
    password = 'secretpass' 
    User.create!(
    :email => email, 
    :password => password, 
    :password_confirmation => password 
) 

    visit '/users/sign_in' 
    fill_in "user_email", :with => email 
    fill_in "user_password", :with => password 
    click_button "Sign in" 

end 

당신이 정말로 이상 (@user = User.find_by) 사용자를 검색 한 다음 테스트의 범위를 외부 변수로 비밀번호를 유지하는 데 필요한 암호를 입력해야하는 경우

.

관련 문제