2012-05-20 29 views
4

我有一个Rails 3.2.3应用程序,我正在使用MiniTest和Capybara来运行我的集成测试。我推出了自己的身份验证,并在我的application_controller.rb中创建了一个current_user helper_method。为什么在测试过程中我的current_user辅助方法不工作?

我的应用程序布局文件只显示某些环节,如注销等,当用户登录。

但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,但是有什么可以防止在测试过程中创建会话?我错过了什么?

回答

3

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

here

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

+2

谢谢!我想这同样适用于MiniTest。但会话散列呢?这是可用的还是我必须在我的登录助手中创建一个?我会给它一个镜头并回报。 – AKWF

相关问题