Home>
Be in trouble

I'm currently learning to raise an exception in an untested branch and catch it
=>rails tutorial chapter 9
Normally, if you catch an exception, it will be RED, if you clear the exception and test it again, it should be GREEN, but even if you clear the exception and test it, it will be RED.

I think the cause is probably user.remember, but I don't know where to fix it. Would you please teach me the solution?

usage environment

OS CentOS8
rails '6.0.3'
AWS cloud9 IDE

test/helpers/sessions_helper_test.rb: 11: in `block in<class: SessionsHelperTest>'
Specific error
# sessions_helper.rb throw an exception on an untested branch
module SessionsHelper
  # Log in as the passed user
  def log_in (user)
    session [: user_id] = user.id
  end
  # Make the user's session persistent
  def remember (user)
    user.remember
    cookies.permanent.signed [: user_id] = user.id
    cookies.permanent [: remember_token] = user.remember_token
  end
  #Returns the user corresponding to the memory token cookie
  def current_user
    if (user_id = session [: user_id])
      @current_user || = User.find_by (id: user_id)
    elsif (user_id = cookies.signed [: user_id])
      raise # intentional exception
      user = User.find_by (id: user_id)
      if user&&user.authenticated? (cookies [: remember_token])
        log_in user
        @current_user = user
      end
    end
  end
  # Returns the currently logged in user (if any)
  def current_user
    if session [: user_id]
      @current_user || = User.find_by (id: session [: user_id])
    end
  end
  # Returns true if the user is logged in, false otherwise
  def logged_in?
    ! current_user.nil?
  end
  #Destroy a persistent sessiondef forget (user)
    user.forget
    cookies.delete (: user_id)
    cookies.delete (: remember_token)
  end
  #Log out the current user
  def log_out
    forget (current_user)
    session.delete (: user_id)
    @current_user = nil
  end
end


rails test =>green

#sessions_helper_test.rb
require'test_helper'
class SessionsHelperTest</pre>
<p>rails test =>red</p>
<pre><code># sessions_helper.rb throw an exception on an untested branch
module SessionsHelper
  # Log in as the passed user
  def log_in (user)
    session [: user_id] = user.id
  end
  # Make the user's session persistent
  def remember (user)
    user.remember
    cookies.permanent.signed [: user_id] = user.id
    cookies.permanent [: remember_token] = user.remember_token
  end
  #Returns the user corresponding to the memory token cookie
  def current_user
    if (user_id = session [: user_id])
      @current_user || = User.find_by (id: user_id)elsif (user_id = cookies.signed [: user_id])
      Remove #raise
      user = User.find_by (id: user_id)
      if user&&user.authenticated? (cookies [: remember_token])
        log_in user
        @current_user = user
      end
    end
  end
  # Returns the currently logged in user (if any)
  def current_user
    if session [: user_id]
      @current_user || = User.find_by (id: session [: user_id])
    end
  end
  # Returns true if the user is logged in, false otherwise
  def logged_in?
    ! current_user.nil?
  end
  #Destroy a persistent session
  def forget (user)
    user.forget
    cookies.delete (: user_id)
    cookies.delete (: remember_token)
  end
  #Log out the current user
  def log_out
    forget (current_user)
    session.delete (: user_id)
    @current_user = nil
  end
end


rails test =>red * It should be green here

Error that occurred

FAIL ["test_current_user_returns_right_user_when_session_is_nil", #<Minitest :: Reportsers :: Suite: 0x0000557eaf8f72c0 @ name = "SessionsHelperTest">, 0.12418821800019941]
 test_current_user_returns_right_user_when_session_is_nil # SessionsHelperTest (0.12s)
        --- expected
        +++ actual
        @@ -1 +1 @@
        -#<User id: 762146111, name: "Michael Example", email: "[email protected]", created_at: "2020-12-17 14:51:23", updated_at: "2020-12-17 14: 51:24 ", password_digest: [FILTERED],

 remember_digest: "$2a $04 $eDh.bNJMPEpfanPpCKmxZeH6NazYRxH.znN3hkIMDvz ...">
What I tried

I checked here for the error, but could not resolve it.

https://www.tutorialfor.com/go.php?id=35527

  • Answer # 1

    https://stackoverrun.com/ja/q/8746642
    As a result of rewriting the test about current_user in sessions_helper_test.rb on this site, the test became GREEN.

    Before rewriting

    require'test_helper'
    class SessionsHelperTest

    After rewriting

    require'test_helper'
    class SessionsHelperTest