From: Stephan Wehner Date: 2009-05-25T08:17:19+09:00 Subject: Re: Test::Unit : assert_aborts James Gray wrote: > On May 24, 2009, at 4:44 AM, Stephan Wehner wrote: > >> let me know. > It's possible to check the message even in old versions of > Test::Unit. For example: > > class SpecificError < RuntimeError; end > > require "test/unit" > > class TestErrorHandling < Test::Unit::TestCase > def test_error_type_and_message > error = assert_raise(SpecificError) do > raise SpecificError, "Magic message goes here..." > end > assert_match(/magic/i, error.message) > end > end > > __END__ > > As for testing for abort(), I wouldn't. What are you really trying to > figure out, if the code would exit with an error message? Then check > that. Throw a StringIO in $stderr and check for a message and see if > Ruby is planning to exit. abort() raises the same Exception exit > does, so just check for that: > > require "test/unit" > > class TestErrorHandling < Test::Unit::TestCase > def test_error_type_and_message > assert_raise(SystemExit) do > abort "Bye." > end > end > end > > __END__ > > Hope that helps. Ok, thanks a lot! You mean something like this. require "test/unit" require 'stringio' class TestErrorHandling < Test::Unit::TestCase def test_error_type_and_message_II assert_aborts(/bye/i) do abort "Bye." end end private def assert_aborts(pattern) save_stderr = $stderr begin $stderr = StringIO.new e = assert_raise(SystemExit) do yield if block_given? end assert_match pattern, e.message ensure $stderr=save_stderr end end end __END__ Stephan > James Edward Gray II -- Posted via http://www.ruby-forum.com/.