0
votes

Working through the Michael Hartl Ruby on Rails 5 tutorial and I am getting an error in testing that I can't seem to figure out. The included screenshot shows debugging output from the test. In particular it show that the 'remember_token' which should getting deleted is behaving oddly. It appears to be deleted and nil if I put debugging code into the forget() function where it happens, but in the integration test it doesn't seem to be completely deleting. It is instead returning "" from the nil? test instead of true/false. The user_id cookie has the same issue, but a cookie such as 'blah' in the test which has never been created properly responds to nil? with true. Any ideas?

The forget method that actually deletes the :remember_token

def forget(user)
  user.forget
  cookies.delete(:user_id)
  cookies.delete(:remember_token)
end

In case the screenshot isn't clear here is the test that is failing:

test "login without remembering" do
  # Log in to set the cookie.
  log_in_as(@user, remember_me: '1')
  delete logout_path
  # Log in again and verify that the cookie is deleted.
  log_in_as(@user, remember_me: '0')
  Rails::logger.debug "**************************************************"
  Rails::logger.debug cookies['blah'].nil?
  Rails::logger.debug cookies['remember_token'].nil?
  Rails::logger.debug "**************************************************"
  assert_nil cookies['remember_token']
end

And here is the output in the logfile:

Completed 302 Found in 2ms (ActiveRecord: 0.1ms)
**************************************************
true

**************************************************

And here is the error message in the test results:

FAIL["test_login_without_remembering", UsersLoginTest, 0.24238487900584005]
test_login_without_remembering#UsersLoginTest (0.24s)
    Expected "" to be nil.
    test/integration/users_login_test.rb:57:in `block in    <class:UsersLoginTest>'

enter image description here

2
Please remove the screenshot and post your code. - Arun Kumar Mohan
what portions of code? or just all code in general? and for what purpose? I'm not sure what the screenshot isn't conveying? Or is it just a general preference for StackOverflow posts? - mgsakata
I couldn't see the code in the screenshot clearly. Btw, please read stackoverflow.com/help/how-to-ask - Arun Kumar Mohan
I didn't see anything in there about screenshots, but I understand your concerns and have posted some of the code and results in question. You should also be able to click on the image to expand it and see it more clearly. - mgsakata

2 Answers

0
votes

The tutorials suggestion for the sessions_controller.rb may need to be changed back to the original suggestion of:

params[:session][:remember_me] == '1' ? remember(user) : forget(user)
0
votes

I'm pretty new at this myself but wouldn't...

assert cookies['remember_token'].blank?

work as it still shows the remember_token is gone and the problem appears to be remember token is now blank and not