Hartl Ruby第8.5章测试失败

时间:2016-02-13 20:45:42

标签: ruby-on-rails ruby

在Hartl教程第8章的最后,我们创建了一个sessions_helper_test.rb来测试"记住" current_user的分支。按照这些步骤,测试应该以绿色方式传递。但是对我来说失败了。没有sessions_helper_test.rb,rake测试通过绿色。所以,这个测试实际上正在发挥作用并找到失败但我不知道如何修复它。

我的test / integration / users_login_test.rb:

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end
  .
  .
  .
  test "login with remembering" do
    log_in_as(@user, remember_me: '1')
    assert_not_nil cookies['remember_token']
  end

  test "login without remembering" do
    log_in_as(@user, remember_me: '0')
    assert_nil cookies['remember_token']
  end
end

这是我的sessions_helper.rb文件:

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

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

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

这是我的sessions_helper_test.rb文件:

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

我的users.yml:

michael:
  name: Michael Example
  email: michael@example.com
  password_digest: <%= User.digest('password') %>

这是rake测试错误:

 FAIL["test_current_user_returns_right_user_when_session_is_nil", SessionsHelperTest, 2016-02-05 21:59:13 -0500]
     test_current_user_returns_right_user_when_session_is_nil#SessionsHelperTest (1454727553.68s)
            --- expected
            +++ actual
            @@ -1 +1 @@
            -#<User id: 762146111, name: "Michael Example", email: "michael@example.com", created_at: "2016-02-13 20:35:28", updated_at: "2016-02-13 20:35:29", password_digest: "$2a$04$JFRB.ADooQvb0.aHFkSAhOM/pgh8fcE7RA55U6poRRp...", remember_digest: "$2a$04$waNBmpt9z5WT22jRgffILuSI/ZL3ZQWQwXtWTPf7rIk...">
            +nil
            test/helpers/sessions_helper_test.rb:11:in `block in <class:SessionsHelperTest>'

2 个答案:

答案 0 :(得分:1)

这可能是格式化错误,我不熟悉Hartl书籍,但是您似乎已经定义了current_user两次,这可能会导致您的问题。

答案 1 :(得分:-1)

在sessions_helper.rb文件中,有两个"def current_user"。 为我工作:

def current_user?
    @current_user ||= User.find_by(id: session[:user_id])
end