加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 为什么我的current_user帮助方法在测试期间

发布时间:2020-12-17 02:17:56 所属栏目:百科 来源:网络整理
导读:我有一个Rails 3.2.3应用程序,我使用MiniTest和Capybara来运行我的集成测试.我推出了自己的身份验证,并在application_controller.rb中创建了current_user helper_method. 我的应用程序布局文件仅在用户登录时显示某些链接,例如注销等. 但是current_user方法
我有一个Rails 3.2.3应用程序,我使用MiniTest和Capybara来运行我的集成测试.我推出了自己的身份验证,并在application_controller.rb中创建了current_user helper_method.

我的应用程序布局文件仅在用户登录时显示某些链接,例如注销等.

但是current_user方法在测试期间不起作用.它看起来像这样:

class ApplicationController < ActionController::Base
  protect_from_forgery
  private

  def authenticate
    if current_user.blank?
      redirect_to root_url,:alert => "You must first log in."
    end
  end

  def authenticate_admin
    unless current_user and current_user.admin?
      redirect_to root_url,:alert => "You must be logged in as an administrator to access     this feature."
    end
  end

  def current_user
    begin
      @current_user ||= User.find(session[:user_id]) if session[:user_id]
    rescue
      nil
    end
  end

  helper_method :current_user
end

所以在我的application.html.erb文件中有:

<% unless current_user.blank? %>
 <li><%= link_to logout_path %></li>

???

所以当我通过浏览器测试它时,这是有效的,但不是在我的测试中. “current_user”最终为零.

我是BDD的新手,但有什么能阻止在测试期间创建会话吗?我错过了什么?

解决方法

NOTE: helper methods defined in controllers are not included.

从here.

解决方案是将它们组织在专用的帮助程序类中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读