Ch 8 Michael Hartl's RoR's tutorial No Method Error: Undefined method

Ch 8 Michael Hartl's RoR's tutorial No Method Error: Undefined method

我一直收到错误消息:

NoMethodError: undefined method `full_title'.

在我的 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
        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

当我删除assert_select "title", full_title("Sign up")和运行 Guard时,测试是绿色的,但是当我把它放回去时,我得到了错误。我该如何解决?

确保在 app/helpers/application_helper.rb:

中定义了 full_title 函数
module ApplicationHelper

  # Returns the full title on a per-page basis. If a page doesn't provide a
  # title, just use the base title.
  def full_title(page_title = '')
    base_title = Rails.application.config.web_app_name
    if page_title.empty?
      base_title
    else
      page_title + " - " + base_title
    end
  end

end

我遇到了同样的问题。我 copy/pasted full_title 方法从 application_helper.rb 到 test_helper.rb,之后它起作用了。希望对您有所帮助!