简体   繁体   中英

Rails Tutorial Failed 9.25 Test Expected “” to be nil

I'm reposting this question because I was having trouble with this part of the rails tutorial as well. I found a different workaround that has the test now passing but I was wondering, since I'm so new at this if it has the same effect?

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 

assert cookies['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. 

If you set a value of a cookies key to nil , it will be interpreted as empty string.

So you have to ask yourself what is the definition of cookies['remember_token'] = "" in your application. If "" is equivalent to nil in your context, then using #blank? for assertion would be just fine.

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