我正在学习前面提到的教程。
当我使用给定命令执行测试时:
bundle exec rake test test=test/helpers/sessions\u helper\u test.rb
我遇到以下错误:
1)错误:
当“记住摘要”错误时,applicationhelpertest“test”当前用户返回“nil”:
命名错误:未定义的方法remember' for #<ApplicationHelperTest:0x000000075bf4d0> test/helpers/sessions_helper_test.rb:7:in设置
2)错误:
applicationhelpertest test_current_user_returns_right_user_with_session_nill:
命名错误:未定义的方法remember' for #<ApplicationHelperTest:0x00000007702400> test/helpers/sessions_helper_test.rb:7:in设置
我在/apps/helpers/session\u helper.rd中定义了一个helper类方法。
--记住方法是这个类的一部分。仍然/test/helpers/sessions\u helper\u测试找不到该方法。
/test/helpers/sessions\u helper\u 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: [email protected]
  password_digest: <%= User.digest('password') %>

archer:
  name: Sterling Archer
  email: [email protected]
  password_digest: <%= User.digest('password') %>

—————————————————————————————————————————————————-
您需要包含helpermodule来测试模块..
在修复了test/helpers/sessions\u helper\u 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

最佳答案

也遵循教程,并有一个类似的问题。通过从以下位置更新类,可以避免使用include:

class ApplicationHelperTest < ActionView::TestCase

到:
class SessionsHelperTest < ActionView::TestCase

关于ruby-on-rails - #<ApplicationHelperTest:0x000000075bf4d0>的未定义方法“记住”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27621524/

10-08 22:59