I've pondered upon this for a very long time already and really couldnt figure out the problem, hope the pros here in stack overflow can enlighten me!
Im doing Michael Hartl Rails tutorial Ch 8, currently stuck at 8.3, I've completed the section but as I log out, my header still renders the logged in header view, which doesn't make sense from the if/else statement inside my _header.html.erb codes.
These are my _header.html.erb codes, which is just a pure copy and paste from the tutorial, I'm pretty sure I've logged out as my integration test has passed.
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: "delete" %>
</li>
</ul>
</li>
<% elsif %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
</header>
my test is as follow:
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
follow_redirect!
assert_select "a[href=?]", login_path <-- line 33
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
and my log out method is defined as follow in my helper file:
def log_out
session.delete(:user_id)
@current_user = nil
end
and my destroy method is defined as follow in my controller file:
def destroy
log_out
redirect_to root_url
end
end
Lastly the error message is as follow,
FAIL["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 0.341018]
test_login_with_valid_information_followed_by_logout#UsersLoginTest (0.34s)
Expected at least 1 element matching "a[href='/login']", found 0.
Expected 0 to be >= 1.
test/integration/users_login_test.rb:33:in `block in <class:UsersLoginTest>'
20/20: [=================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.64770s
20 tests, 52 assertions, 1 failures, 0 errors, 0 skips
[1] guard(main)>
Appreciate if someone can really explain it to me!
_header.html.erb
Change:
To: