简体   繁体   English

Rails教程无法通过9.25测试,预期“”为零

[英]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. 在完成Michael Hartl Ruby on Rails 5教程的过程中,我发现似乎无法弄清的测试错误。 The included screenshot shows debugging output from the test. 随附的屏幕快照显示了测试的调试输出。 In particular it show that the 'remember_token' which should getting deleted is behaving oddly. 特别是它表明应该删除的“ remember_token”的行为异常。 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. 如果我将调试代码放入发生的地方的debug()函数中,则它似乎已删除且为零,但是在集成测试中,它似乎并未完全删除。 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? user_id cookie具有相同的问题,但是在测试中从未正确创建的cookie(例如“ blah”)会响应nil? with true. 与真实。 Any ideas? 有任何想法吗?

The forget method that actually deletes the :remember_token 实际上删除: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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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