[Test::Unit] Asserting exception messages?

D

Daniel Schierbeck

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
 
E

Eric Hodel

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
 
D

Daniel Schierbeck

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top