第8章Michael Hartl的RoR教程没有方法错误:未定义的方法

时间:2015-07-28 02:01:52

标签: ruby-on-rails railstutorial.org

我一直收到错误:

  

NoMethodError:未定义的方法`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时,测试是绿色的,但是当我把它放回去时,我得到了错误。我该如何解决?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我将full_title方法从application_helper.rb复制/粘贴到test_helper.rb,然后它就可以了。希望有所帮助!

答案 1 :(得分:0)

确保你在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