Ruby unittest错误:"你应该要求minitest / autorun'代替&#34。

时间:2015-02-19 03:02:27

标签: ruby unit-testing

我从http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing复制了一个非常简单的单元测试。

require_relative "simple_number"
require "test/unit"

class TestSimpleNumber < Test::Unit::TestCase

    def test_simple
        assert_equal(4, SimpleNumber.new(2).add(2) )
        assert_equal(6, SimpleNumber.new(2).multiply(3) )
    end

end

执行此代码会导致错误:

Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
  /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:3:in `<top (required)>'
  tc_simple_number.rb:5:in `<main>'
MiniTest::Unit::TestCase is now Minitest::Test. From /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit/testcase.rb:8:in `<module:Unit>'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:670:in `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner' (NameError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:255:in `<module:Unit>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:9:in `<module:Test>'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/test/unit.rb:8: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'
    from tc_simple_number.rb:5:in `<main>'

可能出现什么问题?

1 个答案:

答案 0 :(得分:8)

Test :: Unit :: TestCase已弃用,有利于Minitest :: Test。如果你想让你的例子工作,你需要改变一些事情:

  • require "test/unit"替换为require "minitest/autorun"
  • Test::Unit::TestCase with替换为Minitest::Test