LoginSignup
3
1

More than 3 years have passed since last update.

Expected exactly 2 elements matching "a[href="/"]", found 0.. のエラーメッセージ

Last updated at Posted at 2020-10-28

Expected exactly 2 elements matching "a[href="/"]", found 0..のエラーメッセージ



Railsチュートリアルをやっている時にエラーが起きたので、解決した方法を書きます。やっていたのは5章の「5.3.4 リンクのテスト」の所です。
エラーメッセージは

 FAIL["test_layout_links", SiteLayoutTest, 2.2938910419998138]
 test_layout_links#SiteLayoutTest (2.29s)
        Expected exactly 2 elements matching "a[href="/"]", found 1..
        Expected: 2
          Actual: 1
        test/integration/site_layout_test.rb:10:in `block in <class:SiteLayoutTest>'

  1/1: [===================================] 100% Time: 00:00:02, Time: 00:00:02

Finished in 2.29667s
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips

}



私の場合はこのファイルに異常がありました。
app/views/layouts/_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>



この中でも今回は、上記のroot_pathとhelp_pathに' 'がついていたことが原因みたいでした。このエラーが出た原因は、もともとあった'#'にroot_pathなどを入れたことで外し忘れていたみたいでした。
正しくはこちらになります。

        <li><%= link_to "Home",   root_path %></li>
        <li><%= link_to "Help",   help_path %></li>



因みに、app/views/layouts/_fotter.html.erbにも同じようなミスがあったので、上記のエラーが起こった方は確認してみると良いかもしれません。

参照元

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1