简体   繁体   中英

Rails Tutorial Failed 9.25 Test Expected “” to be nil

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>'

在此处输入图片说明

可能需要将sessions_controller.rb的教程建议更改回原始建议:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM