[英]Ruby Unit Tests fail in Eclipse on OS X
I'm finally getting around to learning Ruby using Programming Ruby 1.9. 我终于可以使用Ruby 1.9编程学习Ruby了。 I have installed Ruby using Brew on OS X and it installed Ruby 2.0. 我已经在OS X上使用Brew安装了Ruby,并且安装了Ruby 2.0。
Most of the exercises have worked fine in Eclipse, but when I got to the first example that creates a unit test I started running into trouble. 大多数练习在Eclipse中都运行良好,但是当我到达第一个创建单元测试的示例时,我就开始遇到麻烦了。
In Eclipse, I created a test case like: 在Eclipse中,我创建了一个测试用例,如下所示:
require_relative 'WordsFromString'
require 'minitest/autorun'
class TestWordsFromString < Test::Unit::TestCase
...
end
When I run it as a Ruby Test, I get the following output: 当我将其作为Ruby测试运行时,得到以下输出:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test/unit/ui/console/testrunner (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Applications/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/1276/0/.cp/testing/dltk-testunit-runner.rb:1:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I think there might be something wrong with my basic Ruby installation because if I try and run it from the command line I get: 我认为基本的Ruby安装可能有问题,因为如果我尝试从命令行运行它,则会得到:
Asok:work_freq jeff$ ruby TestWordsFromString.rb
TestWordsFromString.rb:4:in `<main>': uninitialized constant Test (NameError)
EDIT: With the help of Ursus I have figured out that as long as the require and base class are consistent the command line works. 编辑:在Ursus的帮助下,我弄清楚了,只要require和base类是一致的,命令行就可以工作。
So either of the following allow the command-line to successfully execute the test case. 因此,以下任何一项都允许命令行成功执行测试用例。
require 'minitest/autorun'
class TestWordsFromString < MiniTest::Unit::TestCase
or 要么
require 'test/unit'
class TestWordsFromString < Test::Unit::TestCase
So now I just need to get this working inside Eclipse. 所以现在我只需要在Eclipse中运行它即可。
尝试
class TestWordsFromString < Minitest::Test
In searching around for a answer, I came across this Ruby issue that seems to be relevant. 在寻找答案时,我遇到了一个与Ruby有关的问题。 Since I'm new to Ruby I'm not sure I fully understood it, but what I got out of it was that there was an unexpected behavior related to installing the gem test-unit that existed in Ruby 1.9. 由于我是Ruby的新手,所以不确定我是否完全理解它,但是我从中得到的是,与安装Ruby 1.9中存在的gem 测试单元有关的意外行为。 I hadn't installed any gems, so I tried: 我没有安装任何宝石,所以我尝试了:
gem install test-unit
after which the unit test executed successfully in Eclipse and on the command-line. 之后,单元测试将在Eclipse和命令行中成功执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.