2013-02-22 2 views
2

Hartl 's Rails 튜토리얼을 통해 작업하고 있습니다. 내 RSpec에 테스트 케이스 중 하나가 실패 내가 제 7 장까지받은 왔지만, 특히 암호를 다루는 일이 일치하지 않는 :Ruby on Rails 튜토리얼 (Hartl) 7.2.3 RSpec 테스트 실패

describe "has_password? method" do 

    it "should be true if the passwords match" do 
     @user.has_password?(@attr[:password]).should be_true 
    end 

    it "should be false if the passwords don't match" do 
     @user.has_password?("invalid").should be_false 
    end 
end 

출력을 터미널에서 : 실패 :

1) User password encryption has_password? method should be false if the passwords don't match 
Failure/Error: @user.has_password?("invalid").should be_false 
    expected "273725daa81e74764ea1e941a0789da7d580656cd321c64e39d1389f6a7e14d9" to be false 
# ./spec/models/user_spec.rb:111 

나는 내 인생에 무엇이 잘못되었는지 알아낼 수 없습니다

require 'digest' 
class User < ActiveRecord::Base 
attr_accessor :password 
attr_accessible :name, :email, :password, :password_confirmation 

email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

validates :name, :presence => true, 
        :length => { :maximum => 50 } 
validates :email, :presence => true, 
        :format => { :with => email_regex }, 
        :uniqueness => { :case_sensitive => false } 

validates :password, :presence  => true, 
        :confirmation => true, 
        :length  => { :within => 6..40} 


before_save :encrypt_password 

def has_password? (submitted_password) 
    encrypted_password = encrypt(submitted_password) 

end 

private 

    def encrypt_password 
     self.salt = make_salt unless has_password? (password) 
     self.encrypted_password = encrypt(password) 
    end 

    def encrypt(string) 
     secure_hash("#{salt}--#{string}") 
    end 

    def make_salt 
     secure_hash("#{Time.now.utc}--#{password}") 
    end 

    def secure_hash(string) 
     Digest::SHA2.hexdigest(string) 
    end 


end 

: 여기 내 /user.rb 코드입니다. 도움에 감사드립니다! 당신의 has_password? 방법

encrypted_password = encrypt(submitted_password)에서

답변

3

은 읽을 필요가 : encrypted_password == encrypt(submitted_password)

+1

당신은 단지 오 와우 –

+0

:-) 대답으로 날 이길. 나는 문자로 문자를 읽지 않기 위해 내가 얻은 것이라고 생각한다! 감사! –

관련 문제