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 environmentOS 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
Related articles
- ruby - i got stuck in chapter 5 of the rails tutorial i want to apply it because applicationhtmlerb is not applied
- ruby on rails - in the rails tutorial (4th edition, chapter 5), the layout of only the login page is broken
- ruby on rails 6 - rails tutorial chapter 14 about follow, unfollow, following? methods
- ruby - rails tutorial chapter 2 (6th edition)
- ruby on rails - rails tutorial about git config settings
- ruby on rails 6 - rails tutorial (6th edition, chapter 8, list 833) what is the difference between logged_in? and is_logged_in ?
- ruby on rails - rails tutorial chapter 13 cooperation between heroku and aws s3 region
- ruby on rails - phenomenon that the test that should be an "rspec" error passes
- ruby on rails 5 - the routing that i should have set in rails5 resources is not working
- ruby on rails 6 - rails tutorial login [remember me] checkbox test
- ruby - rails tutorial chapter 4 test fails
- ruby - rails tutorial chapter 13 delete does not work in the test of increasing/decreasing the number of posts with rspec
- ruby on rails - i want to write a rails tutorial test in rspec
- ruby on rails 5 - i want to update the received parameters with nil
- ruby on rails - i want to jump to the action of another controller with render
- ruby on rails - heroku: how to check the database_url used for regular backup
- ruby on rails - saved description in nested controller
- ruby on rails - it is not saved in the database after registering the product
- ruby on rails - mvc rails
Trends
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
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
After rewriting