Search code examples
ruby-on-railsrubysessionredisminitest

Integration test, login with session is reset between http requests in the same test


I'm upgrading our application from rails 6.1.7 to 7.0.4 and have encountered an error where the session in the controller is reset after each http request, which did not happen before. This only happens in the test env and not in dev env.

When the user login and we have finished to authentication we set the following:

  def login_user!
    session[:user_id] = @user.id
    .....
  end

When the user tries to visit another link we check the following

 def logged_in?
   session[:user_id].present?
 end

The test:


test 'updates locale' do
  post login_path, params: { email: user.email, password: user.password }, as: :json
  patch user_path(user), params: { user: { locale: 'en' } }, as: :json
end

Usually I use the first line login_path to set the session so that I can test other http requests as metnioned here. However it seems like the session is reset after each call in rails 7. When I do the second request I get the following error: <ActionDispatch::Request::Session:0x25648 not yet loaded>


Solution

  • For future ref to others; This bug was due to that I use the gem redis-session-store which was not compatible with redis version 5 or higher and fakeredis version 0.7