5.6节Hartl教程中的练习4

时间:2015-07-09 19:12:38

标签: ruby-on-rails ruby

我正在尝试完成Hartl教程第5章中的练习4,当我运行$ bundle exec rake test时,我遇到了这两个错误:

Started

ERROR["test_layout_links", SiteLayoutTest, 2015-03-31 09:33:35 +0000]
 test_layout_links#SiteLayoutTest (1427794415.88s)
NoMethodError:         NoMethodError: undefined method `full_title' for #   <SiteLayoutTest:0x00000006cee338>
        test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
    test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'

ERROR["test_full_title_helper", ApplicationHelperTest, 2015-03-31 09:33:35 +0000]
 test_full_title_helper#ApplicationHelperTest (1427794415.99s)
ArgumentError:         ArgumentError: wrong number of arguments (0 for 1)
        app/helpers/application_helper.rb:2:in `full_title'
        test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
    app/helpers/application_helper.rb:2:in `full_title'
    test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'

  7/7:   [====================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.57888s
7 tests, 14 assertions, 0 failures, 2 errors, 0 skips

这些错误意味着什么,以及如何解决错误?

这是我的test / integration / site_layout_test.rb代码:

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
    get signup_path
    assert_select "title", full_title("Sign up")
  end
end

这是我的app / helpers / application_helper.rb代码:

module ApplicationHelper
    def full_title(page_title)
     base_title = "Ruby on Rails Tutorial Sample App"
      if page_title.empty?
        base_title
      else
        " #{page_title} | #{base_title}"
      end
    end
end

这是我的test / helpers / application_helper_test.rb文件:

require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase
  test "full title helper" do
    assert_equal full_title, "Ruby On Rails App"
    assert_equal full_title("Help"), "Help | Ruby On Rails App"
  end
end

2 个答案:

答案 0 :(得分:0)

  

这些错误意味着什么,以及如何解决错误?

NoMethodError: undefined method 'full_title' for #<SiteLayoutTest:0x00000006cee338>
    test/integration/site_layout_test.rb:13

在第13行,您调用了一种不存在的方法。 见http://ruby-doc.org/core-2.2.0/NoMethodError.html

ArgumentError: wrong number of arguments (0 for 1)
     app/helpers/application_helper.rb:2:in 'full_title'
     test/helpers/application_helper_test.rb:5

在第5行,您需要传递一个参数。 见http://ruby-doc.org/core-2.2.0/ArgumentError.html

查看错误消息中行号的位置?

我建议在学习之前(或同时)阅读介绍性红宝书,例如http://ruby-doc.com/docs/ProgrammingRuby/

祝你好运!

答案 1 :(得分:0)

如果有人在通过Michael Hartl第3版时遇到同样的错误......

我在app / helpers / application_helper.rb代码中发现了错误。我需要为page_title添加一个空字符串,更改&#39; def full_title(page_title)&#39; to&#39; def full_title(page_title =&#39;&#39;)&#39;

module ApplicationHelper
    def full_title(page_title = '')
     base_title = "Ruby on Rails Tutorial Sample App"
      if page_title.empty?
        base_title
      else
        "#{page_title} | #{base_title}"
      end
    end
end