【问题标题】:included the module but still getting uninitialized constant error包含模块但仍然出现未初始化的常量错误
【发布时间】:2014-11-29 07:10:21
【问题描述】:

我的 Helper 文件如下所示:

/helpers/application_helper.rb

module ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title)
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      "#{base_title} | #{page_title}"
    end
  end

  # Helper for the sidebar menu highlighting
  def is_active?(page_name)
    if controller_name == "pages"
      "active" if action_name == page_name
    else
      "active" if controller_name == page_name
    end
  end

  # Set correct bootstrap flash messages design
  def bootstrap_class_for flash_type
    case flash_type
      when :success
        "alert-success" # Green
      when :error
        "alert-danger" # Red
      when :alert
        "alert-warning" # Yellow
      when :notice
        "alert-info" # Blue
      else
        flash_type.to_s
    end
  end

end

/support/utilities.rb

include ApplicationHelper

/helpers/application_helper_spec.rb

require 'spec_helper'

describe ApplicationHelper do

  describe "full_title" do

    it "should include the page title" do
      full_title('foo').should =~ /foo/
    end

    it "should include the base title" do
      full_title('foo').should =~ /^Ruby on Rails Tutorial Sample App/
    end

    it "should not include a bar on the home page" do
      full_title('').should_not =~ /\|/
    end
  end
end

我想使用以下规范测试我的应用程序 full_title 助手。我收到 "uninitialized constant ApplicationHelper (NameError)" 错误,但我不明白为什么。我已经包含了 ApplicationHelper 文件。

有人知道哪里出错了吗?

未初始化的常量 ApplicationHelper (NameError)

【问题讨论】:

    标签: ruby-on-rails unit-testing rspec rspec-rails


    【解决方案1】:

    我恢复到 gem 'rspec-rails', '~>2.14.1' 并且它再次工作!

    【讨论】:

    • 根据教程,你应该为 gems 使用准确的版本号,根据教程的在线版本,rspec-rails 应该是:gem 'rspec-rails', '2.13.1' 如果你把它吹掉,你可以期待奇怪的错误。所以我想问题是:“你为什么不真正遵循教程?”你在看书吗?这本书是否使用了比在线教程更新的 gems 版本?
    • 我读了这本书,并按照他们告诉我的做了。现在我正在自己创建一个应用程序,我想学习如何使用更新的 gem。我现在了解到,我可以更好地远离测试版的东西。
    • 好的。祝你好运。 ++ 用于返回并发布您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2012-08-15
    相关资源
    最近更新 更多