Mattias A. wrote in post #1090700:
Hi Damián M. González!
Tanks for your reply, i have found below but it do not answer my
question or I do not know how to read/use it…
I’m kind of new to using test/unit ( and Ruby! ), but I was getting
that same error, too. What I discovered was just adding a test class to
my existing code caused that error until I stripped the code down to
just classes. For example, I wrote a simple Calculator class with four
methods (add, subtract, multiply, divide). I then added code for it to
be interactive. I had to then remove that extra code so that my file
contains only the Calculator class and the TestClass, which included the
test cases, then it worked.
So, I think (and I am guessing here) that your test files should only
include classes to be tested and the actual testing class. That would
make sense, because when testing code, you are mainly concerned with
static test cases which cover all possible execution paths, so why
bother with user interaction.
Here are the two files, the original and the one modified for testing:
######## Original #################
class Calculator
def initialize(a, b)
@a, @b = a, b
end
def add
@a + @b
end
def sub
@a - @b
end
def mul
@a * @b
end
def div
return “undef” if @b == 0
@a / @b
end
end
x, y = ARGV[0].to_i, ARGV[1].to_i
if ARGV.length == 0
print “Usage calc 3 4\n”
exit
end
c = Calculator.new( x, y )
puts “#{x} plus #{y} equals: #{c.add}”
puts “#{x} minus #{y} equals: #{c.sub}\n”
puts “#{x} times #{y} equals: #{c.mul}\n”
puts “#{x} divided by #{y} equals: #{c.div}\n”
######## For Testing #################
require ‘test/unit’
class Calculator
def initialize(a, b)
@a, @b = a, b
end
def add
@a + @b
end
def sub
@a - @b
end
def mul
@a * @b
end
def div
return “undef” if @b == 0
@a / @b
end
end
class TestClass < Test::Unit::TestCase
def test_add
c = Calculator.new( 12, 3 )
assert_equal 15, c.add
end
def test_sub
c = Calculator.new( 12, 3 )
assert_equal 9, c.sub
end
def test_mul
c = Calculator.new( 12, 3 )
assert_equal 36, c.mul
end
def test_div
c = Calculator.new( 12, 3 )
assert_equal 4, c.div
end
end
######### Output ##########
edited to add Output:
Run options:
Running tests:
…
Finished tests in 0.000000s, Inf tests/s, Inf assertions/s.
4 tests, 4 assertions, 0 failures, 0 errors, 0 skips