未定义的方法`记住&#39; for#<applicationhelpertest:0x000000075bf4d0> </applicationhelpertest:0x000000075bf4d0>

时间:2014-12-23 13:38:26

标签: ruby-on-rails ruby

我正在关注https://www.railstutorial.org/book/log_in_log_out提到的教程。

当我使用给定命令执行测试时: bundle exec rake test TEST = test / helpers / sessions_helper_test.rb

我遇到下面给出的错误:

1)错误: ApplicationHelperTest#test_current_user_returns_nil_when_remember_digest_is_wrong: NoMethodError:未定义的方法remember' for #<ApplicationHelperTest:0x000000075bf4d0> test/helpers/sessions_helper_test.rb:7:in设置&#39;

2)错误: ApplicationHelperTest#test_Current_user_returns_right_user_with_session_is_nill: NoMethodError:未定义的方法remember' for #<ApplicationHelperTest:0x00000007702400> test/helpers/sessions_helper_test.rb:7:in设置&#39;


我有一个在/apps/helpers/session_helper.rd中定义的辅助类方法。 --remember方法是这个类的一部分..仍然/ test / helpers / sessions_helper_test无法找到该方法。

/test/helpers/sessions_helper_test.rb文件的代码是

require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase


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

  test "Current_user returns right user with session is nill" 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 current logged-in user (if any).
  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]) #here is an Evaluation Magic for a given Method
        log_in user
        @current_user = user
      end
    end
  end
  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

  # Logs out the current user. # Actions (Set digest to Nill) (Delete the Cookie) (Delete the Session) (Set current_user variable to nill)
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end


  def forget(user)
    user.forget # is to forget the user .. means set th remember_digest to nill -- Go to User Model
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

end

Users.yml文件是:

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

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

archer:
  name: Sterling Archer
  email: duchess@example.gov
  password_digest: <%= User.digest('password') %>

-----------------得到了解决方案-------------------

你需要包含HelperModule来测试模块.. 在test / helpers / sessions_helper_test.rb的修复代码看起来像

之后
require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase
  include SessionsHelper # This line is added to  code.



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

  test "Current_user returns right user with session is nill" 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

1 个答案:

答案 0 :(得分:3)

按照教程进行操作并遇到类似问题。您可以通过以下方式更新您的课程来避免使用包含:

class ApplicationHelperTest < ActionView::TestCase

为:

class SessionsHelperTest < ActionView::TestCase