# Unit testing exception types and messages

**URL:** <https://www.ruby-forum.com/t/unit-testing-exception-types-and-messages/109309>\
**Category:** Ruby\
**Created:** [July 13, 2007, 10:55am UTC](https://www.ruby-forum.com/t/unit-testing-exception-types-and-messages/109309 "2007-07-13T10:55:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [July 13, 2007, 10:55am UTC](https://www.ruby-forum.com/t/unit-testing-exception-types-and-messages/109309/1 "2007-07-13T10:55:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [July 13, 2007, 11:52am UTC](https://www.ruby-forum.com/t/unit-testing-exception-types-and-messages/109309/2 "2007-07-13T11:52:00Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [July 13, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/unit-testing-exception-types-and-messages/109309/3 "2007-07-13T14:38:23Z")

</div>

e = assert\_raise(RuntimeError) { my\_code\_that\_raises }  
assert\_match(/Error message here/i, e.message)

Jason
