Unit testing exception types and messages

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.

<snip>
I do not think that there is a builtin assertion for exception
messages, but defining your assert_raise_message is easy[1].
But I thought I'd point out a small detail:

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

this seem slightly wrong, wouldn't it be
  begin
    # raise SomeException with "some message"
  rescue => e
    assert_equal("some message", e.message)
    assert_instance_of SomeException, e
  end
?
perlish variables are ugly and I think there is no need to raise the
exception again.

[1]
even but I'd like to have a simple way to have filter_backtrace hide my own
custom assertions

···

On Fri, 13 Jul 2007 17:55:01 +0900, Dave Baldwin wrote:

--
goto 10: http://www.goto10.it
blog it: http://riffraff.blogsome.com
blog en: http://www.riffraff.info

e = assert_raise(RuntimeError) { my_code_that_raises }
assert_match(/Error message here/i, e.message)

Jason

···

On 7/13/07, gabriele renzi <rff_rffREMOVE@yahoo.it> wrote:

On Fri, 13 Jul 2007 17:55:01 +0900, Dave Baldwin wrote:

<snip>
I do not think that there is a builtin assertion for exception
messages, but defining your assert_raise_message is easy[1].
But I thought I'd point out a small detail:

> 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

this seem slightly wrong, wouldn't it be
  begin
    # raise SomeException with "some message"
  rescue => e
    assert_equal("some message", e.message)
    assert_instance_of SomeException, e
  end
?
perlish variables are ugly and I think there is no need to raise the
exception again.

[1]
even but I'd like to have a simple way to have filter_backtrace hide my
own
custom assertions

--
goto 10: http://www.goto10.it
blog it: http://riffraff.blogsome.com
blog en: http://www.riffraff.info