samedi 29 octobre 2016

Expected exactly 2 elements matching "a [href="/"]", found 0

I'm trying to check how many links are routed to root_path. My question is why the route in my _header.html.erb file are not counted by assert_select?

(I'm a beginner to code and rails and I'm following Michael Hartl's tutorial)

root_path is used in the page twice:

 <%= link_to "sample app", root_path, id: "logo" %>
 <li><%= link_to "Home",    root_path %></li>

Here is my code for the integration test:

require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a [href=?]", root_path, count: 2
    assert_select "a [href=?]", help_path
    assert_select "a [href=?]", about_path
    assert_select "a [href=?]", contact_path
  end
end

This is the partial code for my HTML file (_header.html.erb):

<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 navbar-right">
        <li><%= link_to "Home",    root_path %></li>
        <li><%= link_to "Help",    help_path %></li>
        <li><%= link_to "Log in", '#' %></li>
      </ul>
    </nav>
  </div>
</header>

When I run $bundle exec rake test:integration, it gives me 1 failure which is:

FAIL["test_layout_links", SiteLayoutTest, 2016-10-20 16:03:19 +0000]
 test_layout_links#SiteLayoutTest (1476979399.42s)
        Expected exactly 2 elements matching "a [href="/"]", found 0..
        Expected: 2
          Actual: 0
        test/integration/site_layout_test.rb:8:in `block in <class:SiteLayoutTest>'

Aucun commentaire:

Enregistrer un commentaire