LoginSignup
0
0

More than 3 years have passed since last update.

[railstutorial]5.3.4 リンクのテストが通らない

Posted at

TL;DR

sample_app/app/views/layouts/_footer.html.erbを確認して
余計なシングルクォーテーションがついていないかチェックする。

得られた教訓
 「コードは間違っていないはずなのになぜかうまくいかない」ときは、
 たいてい文字ではなくて記号の過不足が原因。


問題点

5.3.4「リンクのテスト」のリスト5-32「レイアウトのリンクに対するテスト」においてテストが通らない。

状況

console
rails test:integration
      ↓
1) Failure:
SiteLayoutTest#test_layout_links [/foobar/sample_app/test/integration/site_layout_test.rb:10]:
Expected at least 1 element matching "a[href="/about"]", found 0..
Expected 0 to be >= 1.

1 runs, 4 assertions, 1 failures, 0 errors, 0 skips

解決までにしたこと

  1. consoleにrails sを打ち込む
  2. http://localhost:3000/ にアクセスする
  3. ページ上のAboutをクリックする
  4. Routing Errorが発生 Routing-Error.png
  5. sample_app/app/views/layouts/_footer.html.erbを確認
sample_app/app/views/layouts/_footer.html.erb
<footer class="footer">
  (省略)
  <nav>
    <ul>
      <li><%= link_to "About",   'about_path' %></li>
      <li><%= link_to "Contact", 'contact_path' %></li>
      (省略)
   </ul>
  </nav>
</footer>

原因

link_toメソッドの第2引数に名前付きルートを与えたときに
誤ってシングルクォーテーション(')をつけたままにしていたから。

対策

sample_app/app/views/layouts/_footer.html.erb
<footer class="footer">
  (省略)
  <nav>
    <ul>
     <li><%= link_to "About",    about_path %></li>
     <li><%= link_to "Contact",  contact_path %></li>
      (省略)
   </ul>
  </nav>
</footer>
sample_app/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>
        (省略)
      </ul>
    </nav>
  </div>
</header>
0
0
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
0
0