Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use test/unit with anonymous TestCase

This question belongs zu test-unit version 2.5.3

Problem solved with test-unit version 2.5.4

I have a test with many anonymous TestCases. It worked with test-unit 2.5.0, but the actual version 2.5.3 produces an error.

When I run this test:

gem 'test-unit', ">=2.5.2"
require 'test/unit'
Class.new( Test::Unit::TestCase ) do
  def test_add
    assert_equal( 2, 1+1)
  end
end

no test is executed and I get the error undefined method sub' for nil:NilClass (NoMethodError) in testrunner.rb:361 (I use the actual test-unit-gem 2.5.3).

With a name for the TestCase, the problem disappears:

gem 'test-unit'
require 'test/unit'
X = Class.new( Test::Unit::TestCase ) do
  def test_add
    assert_equal( 2, 1+1)
  end
end

In my real problem, I generate many TestCases. So I have a situation like:

gem 'test-unit'
require 'test/unit'
2.times {
  X = Class.new( Test::Unit::TestCase ) do
    def test_add
      assert_equal( 2, 1+1)
    end
  end
}

If I execute this I get a warning already initialized constant X and the error: comparison of Array with Array failed (ArgumentError) (in collector.rb:48:in sort_by').

My question(s):

  • How can I avoid the error?
  • Or: How can I create TestCases with dynamic assigned constants?
like image 341
knut Avatar asked May 27 '26 15:05

knut


1 Answers

It seems this is down to a change in the latest version of the test-unit gem, which now requires a readable name for a class.

Something like this will work

gem 'test-unit', ">=2.5.2"
require 'test/unit'

Class.new( Test::Unit::TestCase ) do
  def test_add
    assert_equal( 2, 1+1)
  end

  def self.to_s
    "GeneratedClass"
  end

  def self.name
    to_s
  end
end
like image 194
Matthew Rudy Avatar answered May 30 '26 05:05

Matthew Rudy