尝试运行测试时出现未初始化的常量错误

尝试运行测试时出现未初始化的常量错误

本文介绍了尝试运行测试时出现未初始化的常量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新了我的所有 gem,但我发现在尝试运行 Test::Unit 测试时出现错误.我收到下面复制的错误.这来自于创建新的、空的 Rails 项目、搭建一个简单的模型并运行 rake test.

I just updated all my gems and I'm finding that I'm getting errors when trying to run Test::Unit tests. I'm getting the error copied below. That comes from creating new, empty Rails project, scaffolding a simple model, and running rake test.

尝试谷歌搜索未初始化的常量"和 TestResultFailureSupport.我唯一发现的是这个错误报告 从 2007 年开始.

Tried Googling "uninitialized constant" and TestResultFailureSupport. The only thing I found was this bug report from 2007.

我使用的是 OS X.

I'm using OS X.

这些是我在测试停止工作之前更新的 gem:

These are the gems that I updated right before the tests stopped working:

$ sudo gem outdated
Password:
RedCloth (4.2.1 < 4.2.2)
RubyInline (3.8.1 < 3.8.2)
ZenTest (4.1.1 < 4.1.3)
bluecloth (2.0.4 < 2.0.5)
capistrano (2.5.5 < 2.5.8)
haml (2.0.9 < 2.2.1)
hoe (2.2.0 < 2.3.2)
json (1.1.6 < 1.1.7)
mocha (0.9.5 < 0.9.7)
rest-client (1.0.2 < 1.0.3)
thoughtbot-factory_girl (1.2.1 < 1.2.2)
thoughtbot-shoulda (2.10.1 < 2.10.2)

有没有其他人看到过这个问题?任何故障排除建议?

Has anyone else seen this issue? Any troubleshooting suggestions?

我凭直觉将 ZenTest 从 4.1.3 降级到 4.1.1,现在一切又恢复正常了.

On a hunch I downgraded ZenTest from 4.1.3 back to 4.1.1 and now everything works again.

仍然很想知道是否有人看过这个或者有任何有趣的评论或见解.

Still curious to know if anyone else has seen this or has any interesting comments or insights.

$ rake test
(in /Users/me/foo)
/usr/local/bin/ruby -I"lib:test" "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/users_helper_test.rb" "test/unit/user_test.rb"
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant Test::Unit::TestResult::TestResultFailureSupport (NameError)
    from /usr/local/lib/ruby/gems/1.8/gems/test-unit-2.0.2/lib/test/unit/testresult.rb:28
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in `require'
    from /usr/local/lib/ruby/gems/1.8/gems/test-unit-2.0.2/lib/test/unit/ui/testrunnermediator.rb:9
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in `require'
     ... 6 levels...
    from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:214:in `run'
    from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
    from /usr/local/lib/ruby/1.8/test/unit.rb:278
    from /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
/usr/local/bin/ruby -I"lib:test" "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/users_controller_test.rb"

推荐答案

如果模块在嵌套在其中的父模块尚未加载时在单个语句中声明,则可能会发生这种情况.我没有看过那些 gems 中的代码,但我的直觉是这就是正在发生的事情.Chuck 的解决方案表明了这一点.首先调用 gem 'test-unit' 将加载父模块,因此 zen test 的设置最终工作正常.

This can happen if modules are declared in a single statement when the parent module they are nested inside has not yet been loaded. I haven't looked at the code in those gems, but my hunch is that's what is happening. Chuck's solution would suggest that. calling gem 'test-unit' first will load the parent module, so the setup of zen test ends up working ok.

例如

module Foo::Bar
  def do_stuff
    # insert awesomeness here...
  end
end

如果父 Foo 模块尚未定义(例如由另一个 gem)将导致错误

Will result in an error if the parent Foo module hasn't already been defined (e.g. by another gem)

更安全的声明方式是

module Foo
  module Bar
    def do_stuff
      # insert awesomeness here...
    end
  end
end

可能是 Zentest 中需要修补的错误.

May be a bug in Zentest that needs patching.

这篇关于尝试运行测试时出现未初始化的常量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:09