Rails: Login with remembering tests fail one after the other

72 Views Asked by At

This is part of the railstutorial.org for rails 7.I'm testing the correct working of the login with remembering

require 'test_helper'

class SessionsHelperTest < ActionView::TestCase
  def setup
    @user = users(:tony)
    remember(@user)
  end
  
  test 'current user returns right user when session is nil' do
    assert_equal @user,current_user
    assert is_logged_in?
  end

  test 'current user returns nil when remember digest is wrong' do
    @user.update_attribute(:remember_digest,User.digest(User.new_token))
    assert_nil current_user
  end
end

they refers to this helper

  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.encrypted[:user_id])
      
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in(user)
        @current_user = user
      end
    end
  end

the first test fails

 FAIL SessionsHelperTest#test_current_user_returns_right_user_when_session_is_nil (0.10s)
        --- expected
        +++ actual
        @@ -1 +1 @@
        -#<User id: 339078012, name: "tony fusco", email: "[email protected]", created_at: "2022-10-04 07:33:09.980286000 +0000", updated_at: "2022-10-04 07:33:09.980286000 +0000", password_digest: [FILTERED], remember_digest: nil>
        +nil
        test/helpers/sessions_helper_test.rb:10:in `block in <class:SessionsHelperTest>'

and if i comment out part of a line
if user #&& user.authenticated?(cookies[:remember_token])

the second test fails

FAIL SessionsHelperTest#test_current_user_returns_nil_when_remember_digest_is_wrong (2.29s)
        Expected #<User id: 339078012, name: "tony fusco", email: "[email protected]", created_at: "2022-10-04 08:42:46.512404000 +0000", updated_at: "2022-10-04 08:42:48.782076000 +0000", password_digest: [FILTERED], remember_digest: "$2a$04$Haqx77Y7NRV/fCcirQiEU.tZnuqqXGIxD0n81FKTy84..."> to be nil.
        test/helpers/sessions_helper_test.rb:16:in `block in <class:SessionsHelperTest>'

p.s I'm reviewing the other questions about this argument,since they are all different I'm posting this, yet this topic is full of issues

0

There are 0 best solutions below