Ruby-on-rails 尝试运行测试时出现未初始化的常量错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1145318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting uninitialized constant error when trying to run tests
提问by Ethan
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.
我刚刚更新了我所有的 gem,我发现在尝试运行 Test::Unit 测试时出现错误。我收到下面复制的错误。这来自于创建新的、空的 Rails 项目、搭建一个简单的模型并运行rake test.
Tried Googling "uninitialized constant" and TestResultFailureSupport. The only thing I found was this bug reportfrom 2007.
尝试谷歌搜索“未初始化的常量”和 TestResultFailureSupport。我唯一发现的是这个2007 年的错误报告。
I'm using OS X.
我正在使用 OS X。
These are the gems that I updated right before the tests stopped working:
这些是我在测试停止工作之前更新的 gem:
$ 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?
有没有其他人看到过这个问题?任何故障排除建议?
UPDATE
更新
On a hunch I downgraded ZenTest from 4.1.3 back to 4.1.1 and now everything works again.
凭直觉,我将 ZenTest 从 4.1.3 降级到 4.1.1,现在一切又恢复正常了。
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"
采纳答案by madlep
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.
如果模块在嵌套在其中的父模块尚未加载时在单个语句中声明,则可能会发生这种情况。我没有看过那些 gems 中的代码,但我的直觉是这就是正在发生的事情。Chuck 的解决方案表明了这一点。gem 'test-unit'首先调用将加载父模块,因此 zen test 的设置最终可以正常工作。
e.g.
例如
module Foo::Bar
def do_stuff
# insert awesomeness here...
end
end
Will result in an error if the parent Foo module hasn't already been defined (e.g. by another gem)
如果父 Foo 模块尚未定义(例如由另一个 gem),将导致错误
A safer way to declare this is
一种更安全的声明方式是
module Foo
module Bar
def do_stuff
# insert awesomeness here...
end
end
end
May be a bug in Zentest that needs patching.
可能是 Zentest 中需要修补的错误。
回答by Chuck
This apparently comes from using Test::Unit 2.0 with the old Test::Unit. According to Kouhei Sutou on RubyForge, it can be fixedby calling gem 'test-unit'before you require 'test/unit'.
这显然来自将 Test::Unit 2.0 与旧的 Test::Unit 一起使用。根据 RubyForge 上的 Kouhei Sutou 的说法,它可以通过调用gem 'test-unit'before you来修复require 'test/unit'。
回答by Anaderi
As this link suggests http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50it may happen due to preliminary initialization of mocha lib. To prevent it from happeing it is advisable to add line
正如此链接所暗示的http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50可能由于 mocha lib 的初步初始化而发生。为了防止它发生,建议添加行
config.gem 'test-unit', :lib => 'test/unit'
to config/environment.rb
到 config/environment.rb
回答by Matt Scilipoti
Here is the recipe for test_unit 2.0.7 on Rails 2.3.5:
以下是 Rails 2.3.5 上 test_unit 2.0.7 的配方:
In config/environments/test.rb:
在 config/environments/test.rb 中:
config.gem 'test-unit', :version => '2.0.7', :lib => false
In test_helper.rb,
add require 'test/unit',
immediately after require 'test_help'
在 test_helper.rb 中,添加require 'test/unit', 紧随其后require 'test_help'
So it looks like this:
所以它看起来像这样:
require 'test_help'
require 'test/unit'
If you receive this error:
%': one hash required (ArgumentError),
upgrade gem i18nto v0.3.6.
如果您收到此错误:
%': one hash required (ArgumentError),i18n请将gem 升级到 v0.3.6。
回答by Martin Streicher
I ran into this today on Mac OS X 10.6. My solution is as follows:
我今天在 Mac OS X 10.6 上遇到了这个问题。我的解决方案如下:
config.gem 'test-unit', :lib => 'test/unit', :version => '1.2.3' config.gem 'autotest' config.gem 'cucumber' config.gem 'cucumber-rails', :lib => false config.gem 'ffaker', :lib => 'faker' config.gem 'rspec', :lib => false, :version => '>= 1.2.0' config.gem 'rspec-rails', :lib => false, :version => '>= 1.2.0' config.gem 'selenium-client', :lib => 'selenium' config.gem "thoughtbot-factory_girl", :lib => 'factory_girl', :source => "http://gems.github.com" config.gem 'thoughtbot-shoulda', :lib => 'shoulda' config.gem 'webrat' config.gem 'ZenTest', :lib => 'zentest'
回答by Simon Woodside
As with aronchick's comment, for me (OS X 10.6) solution was
与 aronchick 的评论一样,对我来说(OS X 10.6)解决方案是
sudo gem uninstall test-unit
all versions.
所有版本。
回答by scottburton11
I was getting this without mocha or shoulda installed.
我在没有安装 mocha 或 shoulda 的情况下得到了这个。
This post suggests it's due to an incompatibility in test-unit >= 2.0, which I installed as a dependency for the systools gems. Downgrading to 1.2.3 worked for me, a simple require might also.
这篇文章表明这是由于 test-unit >= 2.0 不兼容,我将其安装为 systools gems 的依赖项。降级到 1.2.3 对我有用,一个简单的要求也可能。
回答by Autodidact
Same issue with me too. Nothing above mentioned worked for me except downgrading test-unit back to 1.2.3 I'm missing the coloring of the test-unit 2.x
我也有同样的问题。除了将测试单元降级回 1.2.3 之外,上面提到的任何内容对我都不起作用,我缺少测试单元 2.x 的颜色
回答by Elia Schito
Found the (ugly) solution:
找到了(丑陋的)解决方案:
gem 'unit/test'must be called inside the tests too, not only in the Rakefile.
gem 'unit/test'也必须在测试中调用,而不仅仅是在Rakefile.
回答by radiospiel
You get auto_test to work again with something like
你让 auto_test 再次使用类似的东西
RUBY="ruby -I.:lib:test -rubygems -e 'gem \"test-unit\"'" autotest

