Forum: Ruby ruby unit test error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F09f7d15d9fefc2cf68c4057694c8930?d=identicon&s=25 aidy (Guest)
on 2007-04-01 19:51
(Received via mailing list)
Hi

I am trying to run some unit tests.


<snip>
require 'test/unit'


class UnitTests < Test::Unit::TestCase

  def initialize
    assert(true)
  end

end

UnitTests.new
<snip>

but i am receiving this error

c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:117:in `add_assertion':
undefined method `add_assertion' for nil:NilClass (NoMethodError)

could anyone help?

aidy
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2007-04-01 20:08
(Received via mailing list)
Try this:

require 'test/unit'

class UnitTests < Test::Unit::TestCase
  def test_true
    assert(true)
  end
end

Ryan
944f769c99deff7aa8bc3b5b93830b7a?d=identicon&s=25 Scott Taylor (Guest)
on 2007-04-01 20:12
(Received via mailing list)
You will have to require the file of the class you are testing.

Also, I suspect that you aren't testing a class named "Unit".

If you are testing a class named "Array", your class name would be
"ArrayTest"

Another point to make: I don't think you have to write an
"initialize" method at any point.  You can use setup() and teardown()
(?), and use test_whatever for the actual tests.  I.e.

require 'test/unit'
require 'array'

class ArrayTest < Test::Unit::TestCase
   def setup
  @array = Array.new
   end

   def test_new_array_is_empty
     assert @array.empty?
   end
end

Scott
97e3d8ba68b082414cc3438d15c9d6e2?d=identicon&s=25 Bob Evans (rbe5000)
on 2007-04-01 20:58
(Received via mailing list)
You are calling an assert in the constructor of the Test. The test/
unit framework hasn't setup the necessary stuff yet to call
assertions (namely a TestResult object) - while inside the
constructor, there are no guarantees that the object has been
completely initialized yet.

After you initialize your test, then create a test method and put
asserts in that, as Ryan suggested.

class UnitTest < Test::Unit::TestCase

    def initialize(test_method_name)
  super(test_method_name)
  ...
    end

   def test_unit_method
     assert .....
   end
end

NOTE: if you are creating constructors on your test classes, be aware
that the test/unit framework TestCase class uses constructors with a
string param to identify individual test methods to run when it auto-
builds test suites for you. You will most likely get an error like
the following if you stray from the constructor convention:
/usr/local/lib/ruby/1.8/test/unit/testcase.rb:53:in `initialize':
wrong number of arguments (1 for 0) (ArgumentError)
         from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:53:in `new'
         from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:53:in
`suite'
         from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:52:in
`catch'
         from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:52:in
`suite'
....


Bob
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-04-01 21:17
(Received via mailing list)
On Mon, Apr 02, 2007 at 03:11:16AM +0900, Scott Taylor wrote:
> You will have to require the file of the class you are testing.

He's not testing any external class. He's just writing a skeleton test
at
this point, to check he understands how Test::Unit works (a good
strategy
IMO)

> Also, I suspect that you aren't testing a class named "Unit".
>
> If you are testing a class named "Array", your class name would be
> "ArrayTest"

It doesn't make the slightest difference. He can call his test class
'Flurble' by all means.

Anyway, who says that a unit test suite has to test only a single class?
Many useful tests under Test::Unit are really integration tests that
exercise a whole load of classes.

> Another point to make: I don't think you have to write an
> "initialize" method at any point.

That's true.

I always found it odd about Test::Unit that you define a 'Class' for
your
tests, when presumably only one instance of this class is ever
instantiated.

> >UnitTests.new

He doesn't need that either; the test runner finds all the classes of
interest and creates the objects by itself.

Regards,

Brian.
This topic is locked and can not be replied to.