【问题标题】:Run unit test with Ruby 2.0 and minitest 5.5 without Gemfile在没有 Gemfile 的情况下使用 Ruby 2.0 和 minitest 5.5 运行单元测试
【发布时间】:2015-07-12 06:23:01
【问题描述】:

我是通过阅读 Ruby 编程 来学习 Ruby 的,并且有这个示例代码:

require_relative 'count_frequency'
require_relative 'words_from_string'
require 'test/unit'

class TestWordsFromString < Test::Unit::TestCase
  def test_empty_string
    assert_equal([], words_from_string(''))
    assert_equal [], words_from_string('   ')
  end

  def test_single_word
    assert_equal ['cat'], words_from_string('cat')
    assert_equal ['cat'], words_from_string('   cat   ')
  end

  def test_many_words
    assert_equal ['the', 'cat', 'sat', 'on', 'the', 'cat'],         words_from_string('the cat sat on the mat')
  end

  def test_ignore_punctuation
    assert_equal ['the', "cat's", 'mat'], words_from_string("the cat's mat")
  end
end

当我尝试运行它时,出现了错误: MiniTest::Unit::TestCase is now Minitest::Test. 更详细的错误信息:

我正在使用ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]minitest (5.5.0, 5.4.3, 5.3.5, 4.3.2)。我搜索了一下,发现从minitest5.0开始,MiniTest::Unit::TestCase已经变成了Minitest::Test。但我不能做任何事情,因为它在 gem 的源文件中。有人建议在 Gemfile 中需要 minitest 4.**,但我只是在运行一些脚本。我想不需要 Gemfile。而且我当然不想卸载 minitest5.**。那么有没有办法可以用 minitest5.5 和 ruby​​ 2.0 运行这个脚本?

【问题讨论】:

    标签: ruby unit-testing minitest


    【解决方案1】:

    测试仍应运行。我有相同的设置,即使我得到那个错误,测试也会被执行。

    → ruby --verbose etl_test.rb 
    MiniTest::Unit::TestCase is now Minitest::Test. From etl_test.rb:4:in `<main>'
    Run options: --seed 61653
    
    # Running:
    
    ....
    
    Finished in 0.001316s, 3039.5137 runs/s, 3039.5137 assertions/s.
    
    4 runs, 4 assertions, 0 failures, 0 errors, 0 skips
    
    classyhacker:~/dev/github/exercism/ruby/etl  
    → rbenv versions
      system
      1.9.3-p448
      2.0.0-p451
      2.1.0
      2.1.1
      2.1.2
    * 2.1.3 (set by RBENV_VERSION environment variable)
      jruby-1.7.8
    
    classyhacker:~/dev/github/exercism/ruby/etl  
    → gem list | grep minitest
    minitest (5.5.1, 5.4.3, 4.7.5)
    

    我的测试看起来像

    require 'minitest/autorun'
    require_relative 'etl'
    
    class TransformTest < MiniTest::Unit::TestCase
    
      def test_transform_one_value
        old = { 1 => ['A'] }
        expected = { 'a' => 1 }
    
        assert_equal expected, ETL.transform(old)
      end
    

    require minitest/autorun 也是 ruby​​doc 中的建议方式http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html

    【讨论】:

    • 它确实可以在我的 Mac 上运行,这真的很奇怪。我已经用显示详细错误消息的图片更新了问题。你看一下吗?
    • 我用测试中的 require 语句更新了我的答案,也许您必须更改 require 语句以使用“minitest/autorun”而不是“test/unit”
    • 好的,我自己想通了,我必须要求'minitest/autorun',而不是'test/unit',并将“MiniTest::Unit::TestCase”更改为“Minitest ::Test" 将帮助您摆脱警告。 :)
    • 很高兴了解警告。
    猜你喜欢
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 2016-10-05
    • 1970-01-01
    • 2022-08-22
    • 2010-12-06
    相关资源
    最近更新 更多