如何解决此错误?

时间:2014-12-27 20:17:01

标签: ruby-on-rails ruby

我正在通过迈克尔·哈特尔的Rails教程,我目前正在第8章第8.55节。我正在努力解决我通过该测试接收的失败问题,我无法确定在哪里解决。

FAIL["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 0.05582] test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (0.06s)
    --- expected
    +++ actual
    @@ -1 +1 @@
    -#<User id: 584273342, name: "Kyle Example", email: "kyle@example.com", created_at:             "2014-12-27 20:09:35", updated_at: "2014-12-27 20:09:35", password_digest:    "$2a$04$yK7dSRppfGCqCbrXXZ34meDB2jEulxy9BDVNH32qLDz...", remember_digest:   "$2a$04$GT65nJ.fE90LasXpaT1HruCChf.hl.4fvMrtx2iV48V...">
    +nil
    test/helpers/sessions_helper_test.rb:11:in `block in <class:SessionsHelperTest>'

我的测试代码使用以下代码。

require 'test_helper'

class SessionsHelperTest < ActionView::TestCase

  def setup
    @user = users(:kyle)
    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 

SessionsHelper模块

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  #Remembers a user in a persistent session.
  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

  #Returns the user corresponding to the remember token cookie.
  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

  #Returns the current logged-in user (if any). 
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  #Returns true if the user is logged in, false otherwise. 
  def logged_in?
    !current_user.nil? 
  end

  def log_out
    session.delete(:user_id)
    @current_user = nil
  end

  # Forgets a persistent session.
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end
end

用户模型

class User < ActiveRecord::Base
  attr_accessor :remember_token
  before_save { email.downcase! }
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 }, 
    format: { with: VALID_EMAIL_REGEX },
    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

  # Returns the hash digest of the given string. 
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
      BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  #Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  #Remembers a user in the database for use in persistent sessions 
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  #Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    return false if remember_digest.nil?
    BCrypt::Password.new(remember.digest).is_password?(remember_token)
  end

  # Forgets a user. 
  def forget
    update_attribute(:remember_digest, nil)
  end
end

关于可能导致这种情况的任何想法?

1 个答案:

答案 0 :(得分:5)

你有两个

def current_user

<{1}}

中的

方法

删除这个(旧的,不检查module SessionsHelper存在):

cookies

它目前正在您的测试中使用,因为它定义如下更新。