From: Eric Hodel Date: 2009-05-27T05:47:03+09:00 Subject: Re: Test::Unit : assert_aborts On May 23, 2009, at 21:54, Stephan Wehner wrote: > class Test::Unit::TestCase > def assert_aborts(msg_or_pattern) > asserted = false > caught_exception = 'none' > begin > yield if block_given? # if there is no block, there will not be > any abort > either > rescue AbortException => e > caught_exception = e > if msg_or_pattern.is_a? String > assert_equal msg_or_pattern, e.to_s.sub(/^[a-z_]*: /,'') > return > end > if msg_or_pattern.is_a? Regexp > assert_match msg_or_pattern, e.to_s > return > end > end > flunk "Expected to handle abort with >>#{ msg_or_pattern }<<. > Caught > exception >>#{ caught_exception }<< but didn't handle" > end > end > > module Kernel > def abort(msg) > raise AbortException.new(msg) > end > end You're doing far, far too much work! $ ruby -rstringio -e '$stderr = StringIO.new; begin; abort "hi"; rescue Exception; p $!; end' # Capturing $stderr (then ignoring it) and checking if SystemExit is raised and has the right message is enough: $ cat test.rb require 'minitest/autorun' class TestA < MiniTest::Unit::TestCase def assert_aborts(message) e = assert_raises SystemExit do capture_io do yield end end assert_equal message, e.message end def test_a assert_aborts "hi" do abort "hi" end end end $ ruby19 test.rb Loaded suite test Started . Finished in 0.001654 seconds. 1 tests, 2 assertions, 0 failures, 0 errors, 0 skips