问题描述
我重新发布这个问题是因为我在 Rails 教程的这一部分也遇到了问题。我找到了一种不同的解决方法,现在测试通过了,但我想知道,因为我对这个很陌生,如果它有相同的效果?
user_login_test.rb
require 'test_helper'
class UsersLoginTest < Actiondispatch::IntegrationTest
# I deleted most of the file as Stack kept telling me I was posting too much code and not enough problem.
test 'Login with remember' do
log_in_as(@user,remember_me: '1')
assert_not_nil cookies['remember_token']
end
test 'Login without remember' do
# Log in to set the cookie.
log_in_as(@user,remember_me: '1')
# log in again and verify that the cookie is deleted.
log_in_as(@user,remember_me: '0')
** This seems to be the problem line of code **
assert_nil cookies['remember_token']
end
end ```
I replaced it with
断言 cookie['remember_token'].blank?
test Now passes,but am I allowing a bug by not forcing nil or as long as the cookie is blank then its functionality is the same?
I can add sessions_controller.rb and test_helper.rb if it helps,or any other files that would be helpful.
解决方法
如果您将 cookie 键的值设置为 nil
,它将被解释为空字符串。
因此,您必须问问自己,您的应用程序中 cookies['remember_token'] = ""
的定义是什么。如果 ""
在您的上下文中等同于 nil
,那么使用 #blank?
进行断言就可以了。