From: Eric Hodel Date: 2006-12-03T18:58:00+09:00 Subject: Re: Why does a test fail when I predicted it's exception class? On Dec 2, 2006, at 20:40 , Richard wrote: > You guys were right on! Below is one of my actual test sets, the one > with the exception testing. It is more complicated than the toy I > posted, so it took me a few syntax-errors to get it to work. But then > I got my desired: 17 tests, 17 assertions, 0 failures, 0 errors > > require '.\RevPolishEvaluator.rb' > > class ErrorTests < Test::Unit::TestCase # The succinct way > def test1_noarg; assert_raise(ArgumentError) {Polish.new().eval}; end > def test2_badop; assert_raise(ArgumentError) {Polish.new(%w{2 3 $ > 7 -- > *}).eval}; end > def test3_missingdata; assert_raise(ArgumentError) {Polish.new(%w{2 > +}).eval}; end > def test4_divby0; assert_raise(ZeroDivisionError) {Polish.new(%w{2 0 > /}).eval}; end > end Better: require 'test/unit' require 'rev_polish_evaluator' class TestPolish < Test::Unit::TestCase def test_eval_bad_operator # ... end def test_eval_divide_by_zero # ... end def test_eval_no_input e = assert_raise ArgumentError do Polish.new.eval end assert_equal 'no arguments supplied', e.message end def test_eval_missing_data # ... end end Written this way your tests: * follow the test/unit naming scheme (TestBlah where Blah is the class you're testing) * be in the same order as the output (alphabetical) * will work with the -n flag: $ ruby test_polish.rb -n /eval/ * will work with testrb: $ testrb . -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net I LIT YOUR GEM ON FIRE!