Michael Hartl Tut 3 Ch 9 - UserLoginTest Failure
<p>I am struggling with an error I am getting around the start of chapter 9. When I am running my test, I encounter the following fail state:</p>

<pre><code>..F

Failure:
UsersLoginTest#test_login_with_valid_information_followed_by_logout [C:/webf5201/alcottgriffinj/lab4/test/integration/users_login_test.rb:27]:
Expected exactly 0 elements matching "a[href="/login"]", found 1..
Expected: 0
  Actual: 1
</code></pre>

<p>I have seen the other questions posted on stack regarding this. However, the solutions offered were already present in my code. I started encountering the error once we began to make the changes to the user.rb file in Chapter 9. However, I cannot see what is causing the error myself..</p>

<p><strong>user.rb</strong></p>

<pre><code>class User &lt; ApplicationRecord
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }

  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end
end
</code></pre>

<p><strong>user_login_test.rb</strong></p>

<pre><code>require 'test_helper'

class UsersLoginTest &lt; ActionDispatch::IntegrationTest

    def setup
        @user = users(:michael)
    end 

    test "login with invalid informatiion" do
        get login_path
        assert_template 'sessions/new'
        post login_path, params: {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, params: { 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
        assert_select "a[href=?]", logout_path,         count: 0
        assert_select "a[href=?]", user_path(@user),    count: 0
     end
end
</code></pre>

<p><strong>_header.html.erb</strong></p>

<pre><code>&lt;header class="navbar navbar-fixed-top navbar-inverse"&gt;
  &lt;div class="container"&gt;
    &lt;%= link_to "sample app", root_path, id: "logo" %&gt;
    &lt;nav&gt;
      &lt;ul class="nav navbar-nav navbar-right"&gt;
        &lt;li&gt;&lt;%= link_to "Home", root_path %&gt;&lt;/li&gt;
        &lt;li&gt;&lt;%= link_to "Help", help_path %&gt;&lt;/li&gt;
        &lt;% if logged_in? %&gt;
          &lt;li&gt;&lt;%= link_to "Users", '#' %&gt;&lt;/li&gt;
          &lt;li class="dropdown"&gt;
            &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown"&gt;
              Account &lt;b class="caret"&gt;&lt;/b&gt;
            &lt;/a&gt;
            &lt;ul class="dropdown-menu"&gt;
              &lt;li&gt;&lt;%= link_to "Profile", current_user %&gt;&lt;/li&gt;
              &lt;li&gt;&lt;%= link_to "Settings", '#' %&gt;&lt;/li&gt;
              &lt;li class="divider"&gt;&lt;/li&gt;
              &lt;li&gt;
                &lt;%= link_to "Log out", logout_path, method: "delete" %&gt;
              &lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
        &lt;% else %&gt;
          &lt;li&gt;&lt;%= link_to "Log in", login_path %&gt;&lt;/li&gt;
        &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/div&gt;
&lt;/header&gt;
</code></pre>