我重发这个问题是因为我在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 断言cookies“‘remember_token”。空白吗?
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. 发布于 2021-04-03 10:57:41
如果将cookies键值设置为nil,则会被解释为空字符串。
因此,您必须问问自己,应用程序中cookies['remember_token'] = ""的定义是什么。如果在您的上下文中""等同于nil,那么使用#blank?进行断言将是很好的。
https://stackoverflow.com/questions/66926689
复制相似问题