1

我正在用rails 3.1中的secure_password指令。我正在編寫rspec模型測試,但無法弄清楚如何在rails 3.1中調用secure_password提供的身份驗證方法。Rails 3.1 secure_password和Rspec

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

    describe "it should check credentials" do 
    describe "it should return false on an invalid password" do 
     @user.authenticate("wrongpass").should_be false 
    end 
    end 
end 

有什麼想法嗎?注意:我在我的用戶模型

這裏使用has_secure_password是最新的錯誤我的控制檯給我:

/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/whiny_nil .rb:48:in method_missing': undefined method authenticate'for nil:NilClass(NoMethodError)

回答

0

如果提供的密碼正確或無,則authenticate返回用戶。如果你擁有了一切正確安裝(不要忘記加上password_digest列)這應該工作:

@user.authenticate("wrongpass").should be_nil 

更新:根據您添加的錯誤消息,@user是零,所以你應該檢查。

+0

嘿詹姆斯,謝謝你的回答!不幸的是,它不是。根據github回購,如果驗證失敗,驗證應該返回false,所以false或be_nil都應該工作。我還有一個成功的測試來檢查@user是否響應:password_digest。我會繼續看,並讓你知道。 – sybohy 2011-12-28 06:45:27

+0

請注意,測試「@user respons to:password_digest」測試屬性是db架構的一部分,而不是模型中的has_secure_password行。 – 2013-10-14 03:24:09