How can I use the "test/unit"

A

Artoxvw Artoxvw

when I run the following code

require 'test/unit'
class TC_Pram < Test::Unit::TestCase
def initialize(name)
@name=name
end
def test_array
puts assert_equal(2,@name[2])
end
end
tc=TC_Pram.new("wang")
tc.test_array

there are some errors as follows

c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:125:in `add_assertion':
undefined method `add_assertion' for nil:NilClass (NoMethodError)
from c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:494:in
`_wrap_assertion'
from c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:46:in
`assert_block'
from c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:83:in
`assert_equal'
from test_unit.rb:7:in `test_array'
from test_unit.rb:11

I don't know how to resolve it , so I need your help , thanks
 
D

Daniel Berger

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]
Sent: Thursday, May 14, 2009 7:17 AM
To: ruby-talk ML
Subject: How can I use the "test/unit"

when I run the following code

require 'test/unit'
class TC_Pram < Test::Unit::TestCase
def initialize(name)
@name=name
end
def test_array
puts assert_equal(2,@name[2])
end
end
tc=TC_Pram.new("wang")
tc.test_array

You don't want to use initialize. You want to use setup.

class TC_Pram < Test::Unit::TestCase
def setup
@name = 'wang'
end

def test_array
assert_equal(2, @name[2])
end
end

You don't need to instantiate an instance of TC_Pram. Just run the file.

Regards,

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top