2013-04-29 2 views
0

devise 보석을 사용하여 내 사용자 모델을 테스트하려고합니다. 나는 devise 보석의 rails4 지점에서 뛰고 있습니다. 그리고 최소 암호 길이에 대한 테스트를 작성하려고합니다. 내 user_spec.rb에서RSpec에서 비밀 번호 사용 테스트를 사용하여

는, 내가 쓴 : 그러나

require 'spec_helper' 

describe User do 
    before { @user = User.new(full_name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") } 

    subject { @user } 

    it { should respond_to(:full_name) } 
    it { should respond_to(:email) } 
    it { should respond_to(:password) } 
    it { should respond_to(:password_confirmation) } 
    # it { should ensure_length_of(:password).is_at_least(8) } 

    it { should be_valid } 

    describe 'when full name is not present' do 
    before { @user.full_name = " " } 
    it { should_not be_valid } 
    end 

    describe 'when email is not present' do 
    before { @user.email = " " } 
    it { should_not be_valid } 
    end 

    describe 'when password is not present' do 
    before {@user.password = " "} 
    it { should_not be_valid } 
    end 

    describe 'when password is too short' do 
    it { should ensure_length_of(:password).is_at_least(8) } 
    it { should_not be_valid } 
    end 
end 

, 나는 아직이 장애/오류를 받고 있어요 rspec spec/를 실행하는 경우 :

내가보기로
Failure/Error: it { should be_valid } 
expected #<User id: nil, email: "[email protected]", encrypted_password: 
"$2a$04$/Ifwb1dmfzG6xtBS/amRfOrTTopd8P6JSV48L0G/SWSh...", 
reset_password_token: nil, reset_password_sent_at: nil, 
remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, 
last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, 
created_at: nil, updated_at: nil, full_name: "Example User"> to be valid, 
but got errors: Password is too short (minimum is 8 characters) 
# ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>' 
+0

'foobar'는 6 자입니다. – apneadiving

답변

1

, 당신의 스펙 파일을 잘 작동합니다.

귀하의 it { should be_valid } 시험은 14 호선에서 실패합니다. 암호가 "foobar"로 6 자 길이에 불과하므로 사용자가 무효화됩니다.

before do 
    @user = User.new(
    full_name: "Example User", 
    email: "[email protected]", 
    password: "foobar", 
    password_confirmation: "foobar") 
end 

변경해보십시오 :

before do 
    @user = User.new(
    full_name: "Example User", 
    email: "[email protected]", 
    password: "foobar123", 
    password_confirmation: "foobar123") 
end 
1

당신이 당신의 테스트가 유효한 암호를 생성 한 사용자. 따라서 테스트는 실제로 의도 된 동작을 보장합니다.

테스트 사용자 암호를 "long test password"와 같이 변경하면 문제가 해결됩니다.

감사합니다.