Why is my current_user helper method not working during tests?

I have a Rails 3.2.3 application and I use MiniTest and Capybara to run my integration tests. I performed my own authentication and created the current helper_method_server in my application application_controller.rb.

In my application layout file, only certain links are displayed, for example, logging out, etc., when the user is logged in.

But the current_user method does not work during tests. It looks like this:

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 

So in my application.html.erb file there is:

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

So this works when I test it through a browser, but not in my test. "current_user" ends with zero.

I'm new to BDD, but is there something that prevents session creation during tests? What am I missing?

+4
source share
1 answer

NOTE. Helper methods defined in controllers are not included.

from here .

The solution is to organize them in a dedicated helper class.

+3
source

Source: https://habr.com/ru/post/1413423/


All Articles