4

我有一个 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 的新手,但是有什么东西会阻止在测试期间创建会话吗?我错过了什么?

4

1 回答 1

3

注意:不包括控制器中定义的辅助方法。

这里

解决方案是将它们组织在一个专门的助手类中。

于 2012-05-20T21:30:30.673 回答