【问题标题】:Rails + RSpec - spec/support files not being required; giving "Uninitialized Constant (NameError)"Rails + RSpec - 不需要规范/支持文件;给出“未初始化的常量(NameError)”
【发布时间】:2014-08-29 15:17:03
【问题描述】:

在我当前的项目中,我无法运行任何测试用例,因为我收到以下错误:

/home/harsh/Documents/viburnix/cloud/viburnix/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Features (NameError)

完整的跟踪:

/home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/actionpack-4.1.1/lib/action_dispatch/http/mime_type.rb:163: warning: already initialized constant Mime::ZIP
/home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/actionpack-4.1.1/lib/action_dispatch/http/mime_type.rb:163: warning: previous definition of ZIP was here
Coverage report generated for RSpec to /home/harsh/Documents/viburnix/cloud/viburnix/coverage. 11 / 56 LOC (19.64%) covered.
/home/harsh/Documents/viburnix/cloud/viburnix/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Features (NameError)
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core.rb:112:in `configure'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/support/helpers.rb:1:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/spec_helper.rb:16:in `block in <top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/spec_helper.rb:16:in `each'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/spec_helper.rb:16:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/models/user_spec.rb:1:in `require'
    from /home/harsh/Documents/viburnix/cloud/viburnix/spec/models/user_spec.rb:1:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `load'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `block in load_spec_files'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `each'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/configuration.rb:932:in `load_spec_files'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/command_line.rb:21:in `run'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/runner.rb:100:in `run'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/lib/rspec/core/runner.rb:31:in `invoke'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/gems/rspec-core-3.0.0.beta2/exe/rspec:4:in `<top (required)>'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/bin/rspec:23:in `load'
    from /home/harsh/Documents/viburnix/cloud/viburnix/vendor/ruby/2.1.0/bin/rspec:23:in `<main>'

spec/spec_helper.rbspec:install生成的标准文件,包括:

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

spec/support/helpers.rb,我有:

RSpec.configure do |config|
  config.include Features::SessionHelpers, type: :feature
  config.include Mongoid::Matchers, type: :model
  config.include AuthenticationHelpers, type: :controller
end

它在第 2 行给出错误:Features::SessionHelpers

似乎根本不需要代码,或者只是没有按正确的顺序加载。

问题是在一个 7 人的团队中,我是唯一一个无法运行 spec 的人。非常需要任何帮助或指导,我们将不胜感激。

【问题讨论】:

  • 我刚刚在另一个文件夹中克隆了同一个项目,运行了捆绑安装,但仍然遇到同样的错误。
  • 我刚刚将 Gemfile 和 Gemfile.lock 与我团队的其他 2 位成员进行了比较。 Gemfile 和 lock 版本是相同的。 :(
  • 您是否在spec/support/features/session_helpers.rb 文件中创建了Features::SessionHelpers 类?
  • 这个blog post 可能会有所帮助.. 确保您在某处声明了一个 Features 模块,并且 SessionHelpers 在 Features 模块下的命名空间。
  • @HarshGupta 您能否验证您确实创建了具有正确命名空间的session_helpers 文件?

标签: ruby-on-rails-4 rspec-rails


【解决方案1】:

抱歉回复晚了。这个项目是由另一个团队启动的,是的,所有文件、模块和类都存在于正确的位置。问题是加载/要求(在spec_helper.rb 中)这些文件的顺序。当其他人工作愉快而没有任何问题时,我不得不提前加载/需要一些必需的文件。

让我困惑的是两个问题:

  1. 我并不怀疑它对我不起作用。手动要求这些特定文件是有意义的,并且从一开始就应该是这样。我对它对其他人的作用感到困惑?
  2. 我尝试以与其他人完全相同的方式制作虚拟机。然而我遇到了这个问题,揭穿了这与操作系统或 shell 等有关的神话。

我已将这些行添加到 spec_helper.rb,并在运行测试之后但在提交我的工作之前对它们进行评论。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 2013-11-10
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
相关资源
最近更新 更多