I'm working my way through Chapter 9 of https://www.railstutorial.org/ and am running into trouble with an integration test.
The test looks like this:
class UsersLoginTest < ActionDispatch::IntegrationTest
...
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_not_empty cookies['remember_token']
end
...
end
The call to log_in_as is a function added to ActionDispatch::IntegrationTest in my test_helper, and appears to be working as expected. One of the side effects of this function is that a cookie named 'remember_token' gets set. However, the assertion fails with the message:
FAIL["test_login_with_remembering", UsersLoginTest, 14.115229932998773]
test_login_with_remembering#UsersLoginTest (14.12s)
Expected nil (NilClass) to respond to #empty?.
test/integration/users_login_test.rb:48:in `block in <class:UsersLoginTest>'
Any idea what's going on here? Running the test in the debugger seems to suggest that the cookies object doesn't contain anything that looks like I would expect (there are a couple of cookies that I'm setting in the app, and none of them are appearing). All google turns up is a bunch of people suggesting that it's a bad idea to access cookies in integration tests. I'm running Rails 5.1.2 if that helps.
Rails earlier versions change the behavior, the Rails tutorial seems to be deprecated due to an older version Rails.
To pass tests you can forget helper like:
class UsersLoginTest < ActionDispatch::IntegrationTest
...
test "remember if checked in login" do
post login_path, params: { session: { email: @user.email, password: "password", remember_me: '1'}}
assert_not_empty cookies[:remember_token]
end
test "no remember if not checked in login" do
post login_path, params: { session: { email: @user.email, password: "password", remember_me: '1'}}
post login_path, params: { session: { email: @user.email, password: "password", remember_me: '0'}}
assert_empty cookies[:remember_token]
end
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With