From: Paul Brannan Date: 2001-11-10T00:04:47+09:00 Subject: [ruby-talk:24760] Re: assert_exception_and_message On Fri, 9 Nov 2001, Robert Feldt wrote: > Hi, > > I didn't find a way in RubyUnit to check both an exception and its error > message. Maybe its there somewhere. If not here's one version: > > def assert_exception_and_message(exception, eMessage, message = "") > exception_raised = true > begin > yield > exception_raised = false > rescue Exception => e > raise e unless exception === e > assert_equals(eMessage, e.message) > end > assert(exception_raised, message) > end Another way to write this is to just use assert_exception, and provide a default parameter. It's also useful to be able to provide a regular expression instead of having to provide the exact string: def assert_exception(exception, message="", pattern=//) exception_raised = true begin yield exception_raised = false rescue Exception => e raise e, e.message, e.backtrace unless exception === e assert(pattern === e.message, "") end assert(exception_raised, message) end > Its not perfect but can easily be fixed. If something similar is not in > Lapidary I'd propose its added to the merged Test::Unit. I think I agree. I use something similar to this often. Paul