Chris.Zou
Chris.Zou

Reputation: 4576

Run unit test with Ruby 2.0 and minitest 5.5 without Gemfile

I was learning Ruby by reading Programming Ruby and there is this example code:

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

When I tried to run it, an error occured: MiniTest::Unit::TestCase is now Minitest::Test. More detailed error message: more detailed error msg

I'm using ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14] and minitest (5.5.0, 5.4.3, 5.3.5, 4.3.2). I did some search and found that since minitest5.0, MiniTest::Unit::TestCase has changed to Minitest::Test. But I cannot do anything since it's in the source file of the gem. Some suggests that require minitest 4.** in Gemfile, but i'm just running a few scripts. There is no need for Gemfile I suppose. And I certainly don't want to uninstalling minitest5.**. So is there a way I could run this script with minitest5.5 and ruby 2.0?

Upvotes: 2

Views: 367

Answers (1)

aarti
aarti

Reputation: 2865

The tests should still run. I have the same set up and even though I get that error, the tests are executed.

→ 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)

My test looks like

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 is also the suggested way in rubydoc http://ruby-doc.org/stdlib-2.0.0/libdoc/minitest/rdoc/MiniTest.html

Upvotes: 1

Related Questions