2014-01-08 3 views
0

나는 Michael Hartl의 튜토리얼을 따르고있다. rspec 테스트가 작동하는 데 문제가 있습니다. 암호의 암호화 된 값이 일관성이 없기 때문에 오류가 발생합니다. 결과가 일치하지 않기 때문에 수수께끼입니다. 때로는 마이그레이션을 롤백하고 다시 마이그레이션하고 레이크 테스트를 준비하고 테스트를 다시 실행할 때 테스트가 통과됩니다. 때때로 그들은 여전히 ​​실패합니다. 이 문제가 발생했거나 잘못 구현 했습니까? 때로는 테스트가 롤백, 마이그레이션 후 통과,rspec failed with password_digest

$ rspec spec/ 
.........F................... 
Failures: 

1) User return value of authenticate method with valid password 
Failure/Error: it { found_user.authenticate(@user.password).should eq @user } 

    expected: #<User id: nil, name: "Example User", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$04$ZSegpX1GY1RIcqvDmM4FteBQ.HXzHM2pZtj0mwobetfs..."> 
     got: #<User id: 1, name: "Example User", email: "[email protected]", created_at: "2014-01-08 09:35:31", updated_at: "2014-01-08 09:35:31", password_digest: "$2a$04$qO1QSOwTEYtioSSmNObKKOgc5K3SQyU4dDk.APqN1egI..."> 

    (compared using ==) 

    Diff: 
    @@ -1,2 +1,2 @@ 
    -#<User id: nil, name: "Example User", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$04$ZSegpX1GY1RIcqvDmM4FteBQ.HXzHM2pZtj0mwobetfs..."> 
    +#<User id: 1, name: "Example User", email: "[email protected]", created_at: "2014-01-08 09:35:31", updated_at: "2014-01-08 09:35:31", password_digest: "$2a$04$qO1QSOwTEYtioSSmNObKKOgc5K3SQyU4dDk.APqN1egI..."> 

# ./spec/models/user_spec.rb:82:in `block (4 levels) in <top (required)>' 

Finished in 0.38035 seconds 
29 examples, 1 failure 

Failed examples: 

rspec ./spec/models/user_spec.rb:82 # User return value of authenticate method with valid password 

Randomized with seed 23961 

을 다시 한번 : 내가 얻을

describe "return value of authenticate method" do 
    before do 
     @user.save 
    end 
    let(:found_user) { User.find_by(email: @user.email) } #found_user should be the user fetched from the database 

    describe "with valid password" do 
     it { found_user.authenticate(@user.password).should eq @user } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("mismatch") } 

     it { should_not eq user_for_invalid_password } 
     specify { expect(user_for_invalid_password).to be_false } 
    end 
end 

그리고 샘플 오류 메시지 : 여기

는 user_spec.rb 의 코드의 특정 조각입니다 , 테스트 데이터베이스를 다시 설정합니다. 때로는 여전히 실패합니다. 시간 내 줘서 고마워.

before do 
    @user = User.new(name: "Example User", email: "[email protected]", password: "foobar2", password_confirmation: "foobar2") 
end 
subject{user} 

레이크 사용 : -

업데이트 여기 내가 @user 초기화 어떻게 문제를 해결하지 않는 것 재설정, 시험은 여전히 ​​때때로 실패합니다. 무엇 rake db:migrate:status shows?

+1

보다는 subject { @user }을 시도해야 나에게 보인다 @ user' 변수가 있습니까? – phoet

답변

0

나는 완전히 마이그레이션 할 다음 DB를 제거해야 좋습니다. 그것은 당신의 DB에 이미 저장된 사용자의 passwd를 그런 다음

rake db:migrate 
0

을 실행, 완전히

rake db:reset 

를 제거되지 않습니다 당신은`않습니다 subject { user }

관련 문제