Dave Baldwin
7/13/2007 8:55:00 AM
Testing that an exception of the given type is raised is easy with
assert_raise:
assert_raise(SomeExceptoin) {raise SomeExceptoin, "some message"}
but if I want to also test the exception message how do I do this?
My first though was to try:
assert_raise(SomeExceptoin) {raise SomeExceptoin, "some message"}
assert_equal( "some message", $!.message)
relying on $! to hold the information message from the last raise,
but this doesn't work as assert_raise sets $! to nil somewhere along
the line.
I can use an idiom like:
begin
raise SomeExceptoin, "some message"
rescue
assert_equal("some message", $!.message)
assert_raise(SomeExceptoin) {raise SomeExceptoin, "some message"}
end
in the tests, but this isn't very DRY. This could be improved by
inventing a assert_raise_with_message method to hide this, but is
there a way already defined in the unit testing framework to handle
this?
Thanks,
Dave.