【问题标题】:Ch 8 Michael Hartl's RoR's tutorial No Method Error: Undefined method第 8 章 Michael Hartl 的 RoR 教程 No Method Error: Undefined method
【发布时间】:2016-05-21 09:55:33
【问题描述】:

我不断收到错误:

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

【问题讨论】:

标签: ruby-on-rails railstutorial.org


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢! - 正如你所建议的,我看了这个。它已定义,但为了仔细检查,我直接从书中复制了代码 - 我仍然收到错误。
    • 如果您按照教程进行操作,则此测试不应该检查标题。删除该行,测试会正确检查链接。在 test\controllers\static_pages_controller_test.rb 中有一个检查标题的测试如果你希望你的布局链接测试也测试标题,有两个问题。首先是测试不知道full_title,它在模块ApplicationHelper中定义。你必须包括它。第二个问题是测试获取了root_path,但正在检查“注册”的标题,这可能不是主页的正确标题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多