[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby unit test error

aidy

4/1/2007 5:47:00 PM

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

4 Answers

Ryan Leavengood

4/1/2007 6:07:00 PM

0

Try this:

require 'test/unit'

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

Ryan

Scott Taylor

4/1/2007 6:11:00 PM

0


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



On Apr 1, 2007, at 1:50 PM, aidy wrote:

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


Robert Evans

4/1/2007 6:57:00 PM

0

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


On Apr 1, 2007, at 10:50 AM, aidy wrote:

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


Brian Candler

4/1/2007 7:16:00 PM

0

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.