Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [Test::Unit] Asserting exception messages?

Reply
Thread Tools

[Test::Unit] Asserting exception messages?

 
 
Daniel Schierbeck
Guest
Posts: n/a
 
      02-16-2007
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 Schierbeck


 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      02-16-2007
On Feb 16, 2007, at 11:36, Daniel Schierbeck 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

 
Reply With Quote
 
 
 
 
Daniel Schierbeck
Guest
Posts: n/a
 
      02-17-2007
On Sat, 2007-02-17 at 06:17 +0900, Eric Hodel wrote:
> On Feb 16, 2007, at 11:36, Daniel Schierbeck 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


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

Thanks!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asserting nothings thrown in a destructor g3rc4n@gmail.com C++ 6 12-13-2008 04:21 PM
Asserting IRQs paswenson VHDL 0 01-23-2008 11:37 PM
How to aggregate differences instead of asserting just the first difference ? tomjbr.16324861@bloglines.com Java 2 09-16-2005 07:14 AM
JUnit - How to aggregate differences instead of asserting just the first difference ? tomjbr.16324861@bloglines.com Java 0 09-14-2005 03:01 PM
Asserting files are not different in Test::Unit? David Brady Ruby 2 08-26-2005 10:36 PM



Advertisments