2011-11-11 7 views
2

私はちょうどレールを使い始めました。M. Hartlの "Ruby on Railsチュートリアル"に従うことにしました。良いイントロのようだ。パスワード:Ruby on Railsチュートリアル:M.Hartl

私はナットを駆動している失敗したテストに入っています。 私は「has_password」メソッドの仕事上の私は条件を変更しようとしている2.7.0

RSpecの、そしてテストで、レール3.1.1を実行しています。

失敗テスト:

 
1) User password encryption authenticate method should return the user on email/password match 
Failure/Error: matching_user.should == @user 
expected: # 
     got: nil (using ==) 
# ./spec/models/user_spec.rb:149:in `block (4 levels) in ' 

RSpecのテスト:Userモデルで

describe User do 

before(:each) do 
@attr = {:name => 'testing', 
     :email =>'[email protected]', 
     :password => "testtest", 
     :password_confirmation => "testtest"} 
end 

... 

describe "password encryption" do 
    before(:each) do 
    @user = User.create!(@attr) 
    end 

... 

describe "authenticate method" do 

    it "should exist" do 
    User.should respond_to(:authenticate) 
    end 

    it "should return nil on email/password mismatch" do 
    User.authenticate(@attr[:email], "wrongpass").should be_nil 
    end 

    it "should return nil for an email address with no user" do 
    User.authenticate("[email protected]", @attr[:password]).should be_nil 
    end 

    it "should return the user on email/password match" do 
    matching_user = User.authenticate(@attr[:email], @attr[:password]) 
    matching_user.should == @user 
    end  
end 

... 

def has_password?(submitted_password) 
    encrypt_password == encrypt(submitted_password) 
end 

def self.authenticate(email, submitted_password) 
    user = find_by_email(email) #self.where("email = ?", email) 
    return nil if user.nil? 
    return user if user.has_password?(submitted_password) 
end 

private 

def encrypt_password 
    self.salt = make_salt if new_record? 
    self.encrypted_password = encrypt(password) 
end 

は、私がここで間違ってやっているかを把握することはできません。あなたの失敗の仕様で

+0

この書籍には、明らかにフォーラムがあります。これは役に立ちました: http://getsatisfaction.com/railstutorial – Slamice

答えて

1

あなたはそれがnilに設定されていますので

matching_user.should == @user 

しかし@userがどこにも定義されていません持っています。

編集:失敗した仕様に次のputsを追加

試してみて、あなたがそれを実行した後、あなたのスペック出力の取得結果何を参照してください。

it "should return the user on email/password match" do 
    matching_user = User.authenticate(@attr[:email], @attr[:password]) 
    puts matching_user # add this 
    puts @user   # and also this 
    matching_user.should == @user 
end  
+0

ありがとうございましたが、もともと私はこの投稿の長さのためにユーザーを作成したコードを省略しました。私はそれを加えた。それと同じ問題。 – Slamice

+0

ありがとう!あなたのデバッグの助けを借りて、私は私のhas_passwordメソッドに誤字があることを発見しました: 'def has_password?(submitted_pa​​ssword) encrypted_pa​​ssword == encrypt(submitted_pa​​ssword) end' – Slamice

関連する問題