From: Martin DeMello Date: 2012-04-30T11:15:52+09:00 Subject: Re: Need help with unit test raise "string" does raise a RuntimeError. If you want a custom exception class, you need to create it first: ruby-1.9.3-p0 > begin; raise "hello world"; rescue Exception => e; puts "rescued #{e.class}"; end rescued RuntimeError ruby-1.9.3-p0 > class BadValue < Exception; end ruby-1.9.3-p0 > begin; raise BadValue.new("hello world"); rescue Exception => e; puts "rescued #{e.class}"; end rescued BadValue martin On Sun, Apr 29, 2012 at 6:35 PM, Thi N. wrote: > Hi all, > I'm a newbie to Ruby. I tried the below code but don't understand why > the "test_typecheck" does not give any error. Could someone shed a light > to my problem ? > thanks > tony > > > test1.rb > ======== > > require 'test/unit' > > class SimpleNumber > >  def initialize( num ) >    raise "input must be a number" unless num.is_a?(Numeric) >    @x = num >  end > >  def add( y ) >    @x + y >  end > >  def multiply( y ) >    @x * y >  end > > end > > #error = SimpleNumber.new('abc') <== this raises exeption > > > class TestSimpleNumber < Test::Unit::TestCase > >  def test_simple >    assert_equal(4, SimpleNumber.new(2).add(2) ) >    assert_equal(4, SimpleNumber.new(2).multiply(2) ) >  end > >  def test_typecheck >    assert_raise( RuntimeError ) { SimpleNumber.new('abc') } >  end > end > > > > ===================== > I would expect to see assertion failure for test_typecheck but it does > not give any. > > output > ====== > > Run options: > > # Running tests: > > .. > > Finished tests in 0.006836s, 292.5688 tests/s, 438.8531 assertions/s. > > 2 tests, 3 assertions, 0 failures, 0 errors, 0 skips > > -- > Posted via http://www.ruby-forum.com/. >