Reputation: 539
I am working on rails tutorial by Michael Hartl's (chapter 9).
I get this error:
Error:
UsersLoginTest#test_login_with_remembering:
NoMethodError: undefined method `FILL_IN' for nil:NilClass
test/integration/users_login_test.rb:42:in `block in <class:UsersLoginTest>'
FILL-IN was added in chapter 8 and there wasn't any problem till chapter 9. I tried a lot but I can't fix it. Here is my UsersLoginTest class.
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path, session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
test "login with valid information followed by logout" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
# Simulate a user clicking logout in a second window.
delete logout_path
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_equal assigns(:user).FILL_IN, FILL_IN
end
test "login without remembering" do
log_in_as(@user, remember_me: '0')
assert_nil cookies['remember_token']
end
end
Line 42 is:
assert_equal assigns(:user).FILL_IN,FILL_IN
Upvotes: 0
Views: 2564
Reputation: 303
You need to use an @user instance variable in listing 9.29 if you did the "login with remembering exercise" in listing 8.62. It took me a while to realize this, so it might help other beginners to see what Shadwell's suggestions look like in the code:
sessions_controller.rb
class SessionsController < ApplicationController
# ...
def create
@user = User.find_by(email: params[:session][:email].downcase)
if @user && @user.authenticate(params[:session][:password])
log_in @user
params[:session][:remember_me] == '1' ? remember(@user) : forget(@user)
redirect_back_or @user
else
flash.now[:danger] = 'Invalid email/password combination' # Flash.now designed for rendered pages, disappears at the next request.
render 'new'
end
end
# ...
end
users_login_test.rb
# ...
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_equal assigns(:user).remember_token, cookies['remember_token']
end
# ...
To be fair, Michael Hartl advises us to solve the exercises in separate topic branches with git.
Upvotes: 8
Reputation: 34774
The errors shows that assigns(:user)
is returning nil.
assigns
tests for an instance variable set in your controller. The expectation here is that you have a value set for @user
in your controller and that when the method FILL_IN
is called on it that it will equal FILL_IN
. Once you've fixed the problem of @user
being nil
you'll need to actually fill in those placeholders.
Upvotes: 3
Reputation: 2629
FILL_IN
in this case is a placeholder. Michael expects you to replace this with the correct text.
Upvotes: 1