(u_u)

[rails tutorial] "undefined local variable or method `root_path' for SiteLayoutTest:Class (NameError)"

Categories: [blog]
Tags: [rails], [ruby]

間違い
require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest
  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
takuya@takuya-PRIMERGY-TX1310-M3:~/environment/sample_a$ rails test
Running via Spring preloader in process 21977
Traceback (most recent call last):
        28: from -e:1:in `<main>'
        27: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        26: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
        25: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
        24: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
        23: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
        22: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
        21: from /home/takuya/environment/sample_app/bin/rails:9:in `<top (required)>'
        20: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
        19: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
        18: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
        17: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
        16: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>'
        15: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/command.rb:44:in `invoke'
        14: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/command/base.rb:63:in `perform'
        13: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
        12: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
        11: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
        10: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/commands/test/test_command.rb:38:in `perform'
         9: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/test_unit/runner.rb:39:in `run'
         8: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/test_unit/runner.rb:50:in `load_tests'
         7: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/test_unit/runner.rb:50:in `each'
         6: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/railties-5.1.6/lib/rails/test_unit/runner.rb:50:in `block in load_tests'
         5: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
         4: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
         3: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
         2: from /home/takuya/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
         1: from /home/takuya/environment/sample_app/test/integration/site_layout_test.rb:3:in `<top (required)>'
/home/takuya/environment/sample_app/test/integration/site_layout_test.rb:4:in `<class:SiteLayoutTest>': undefined local variable or method `root_path' for SiteLayoutTest:Class (NameError)

正解

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
takuya@takuya-PRIMERGY-TX1310-M3:~/environment/sample_app$ rails test
Running via Spring preloader in process 22785
Run options: --seed 15986

# Running:

....F

Finished in 4.137880s, 1.2083 runs/s, 2.4167 assertions/s.

  1) Failure:
SiteLayoutTest#test_layout_links [/home/takuya/environment/sample_app/test/integration/site_layout_test.rb:7]:
Expected exactly 2 elements matching "a[href="/"]", found 1..
Expected: 2
  Actual: 1

5 runs, 10 assertions, 1 failures, 0 errors, 0 skips

ん、おかしい

修正

_header.html.erb 間違い

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", '#', 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>

正解

<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>
takuya@takuya-PRIMERGY-TX1310-M3:~/environment/sample_app$ rails test
Running via Spring preloader in process 23358
Run options: --seed 54211

# Running:

.....

Finished in 0.439393s, 11.3793 runs/s, 29.5863 assertions/s.

5 runs, 13 assertions, 0 failures, 0 errors, 0 skips