# Asserting exception messages?

**URL:** <https://www.ruby-forum.com/t/asserting-exception-messages/85856>\
**Category:** Ruby\
**Created:** [February 16, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/asserting-exception-messages/85856 "2007-02-16T20:36:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 16, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/asserting-exception-messages/85856/1 "2007-02-16T20:36:53Z")

</div>

I’ve been looking around, but I haven’t found a simple way of asserting  
that a raised exception has a specific message. RSpec can do this quite  
easily:

```
lambda{ @fixture.foo }.should_raise(FooError,
   'out of foo, try our delicious bar instead!')

```

I know I can do this:

```
def test_raises_foo_with_correct_message
  @fixture.foo
rescue FooError => e
  unless e.message == 'out of foo, try our delicious bar instead!'
    flunk 'raised with wrong message'
  end
else
  flunk 'did not raise exception'
end

```

But that’s rather inelegant. This is what I’d like to have:

assert\_raise FooException, BarException,  
‘out of foo and bar. try baz!’ do  
@fixture.foo  
end

So what’s your thoughts?

Cheers,  
Daniel S.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 16, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/asserting-exception-messages/85856/2 "2007-02-16T22:18:14Z")

</div>

On Feb 16, 2007, at 11:36, Daniel S. wrote:

> I’ve been looking around, but I haven’t found a simple way of  
> asserting  
> that a raised exception has a specific message. RSpec can do this  
> quite  
> easily:
> 
> ```
> lambda{ @fixture.foo }.should_raise(FooError,
> 'out of foo, try our delicious bar instead!')
> 
> ```

So does Test::Unit.

e = assert\_raise BlahError do … end  
assert\_equal ‘something’, e.message

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [February 17, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/asserting-exception-messages/85856/3 "2007-02-17T15:21:32Z")

</div>

On Sat, 2007-02-17 at 06:17 +0900, Eric H. wrote:

> So does Test::Unit.
> 
> e = assert\_raise BlahError do … end  
> assert\_equal ‘something’, e.message

Very nice, didn’t think about checking the return value!

Thanks!
