Rails 关于集成测试与 helper 中的实例变量的疑问

wwwicbd · May 04, 2017 · 1165 hits

app/helpers/sessions_helper.rb

module SessionsHelper
  def log_in(user)
    session[:user_id] = user.id
  end

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

  def logged_in?
    !current_user.nil?
  end

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

app/controllers/sessions_controller.rb

class SessionsController < ApplicationController
  def destroy
    log_out
    redirect_to root_url
  end
end

application_controller.rbtest_helper.rb 中都 include 了上面的 SessionsHelper, 发生了一些奇怪的事情:

集成测试中

delete logout_path
assert_not logged_in? # 这里出错, @current_user 取到了值

在 point1 和 point2 处打印@current_user 却取不到。

麻烦帮忙解释一下原因,谢谢

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.