会话助手测试失败

时间:2015-09-08 23:06:51

标签: ruby-on-rails railstutorial.org

从Michael Hartl的书中,我有以下会话帮助测试:

require 'test_helper'

class SessionsHelperTest < ActionView::TestCase

 def setup
   @user = users(:michael)
   remember(@user)
 end

 test "current_user returns right user when session is nil" do
  assert_equal @user, current_user
  assert is_logged_in?
 end


 test "current_user returns nil when remember digest is wrong" do
  @user.update_attribute(:remember_digest, User.digest(User.new_token))
  assert_nil current_user
 end
end

我已将当前用户定义为以下

def current_user
  if (user_id = session[:user_id])
    @current_user ||= User.find_by(id: user_id)
  elsif (user_id = cookies.signed[:user_id])
    user = User.find_by(id: user_id)
    if user && user.authenticated?(cookies[:remember_token])
      log_in user
      @current_user = user
    end
  end
end

但是,运行此测试时,我收到以下错误:

SessionsHelperTest#test_current_user_returns_right_user_when_session_is_nil           [/Users/Sites/sample_app/sample_app/test/helpers/sessions_helper_test.rb:11]:
--- expected
+++ actual
@@ -1 +1 @@
-#<User id: 762146111, name: "Michael Example", email: "michael@example.com", created_at: "2015-09-08 22:46:47", updated_at: "2015-09-08 22:46:48", password_digest: "$2a$04$NCAY0E57aRYQgbjBrSEHZusy5MadB2eMHo5SEr1rw7n...", remember_digest: "$2a$04$CeGDjrj0haJ36jeIvi5.X.FqNzEVsrIkoPaxUs/ShAW...">
+nil

关于我如何解决这个问题的任何想法?

0 个答案:

没有答案