[Test::Unit] Asserting exception messages?

Discussion in 'Ruby' started by Daniel Schierbeck, Feb 16, 2007.

  1. 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
     
    Daniel Schierbeck, Feb 16, 2007
    #1
    1. Advertising

  2. Daniel Schierbeck

    Eric Hodel Guest

    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
     
    Eric Hodel, Feb 16, 2007
    #2
    1. Advertising

  3. 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!
     
    Daniel Schierbeck, Feb 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Brady
    Replies:
    2
    Views:
    157
    Austin Ziegler
    Aug 26, 2005
  2. Esad Hajdarevic

    asserting in code vs unit tests

    Esad Hajdarevic, Mar 5, 2006, in forum: Ruby
    Replies:
    12
    Views:
    228
    chiaro scuro
    Mar 6, 2006
  3. Curt Sampson
    Replies:
    3
    Views:
    117
    Phlip
    Aug 11, 2007
  4. aidy

    Test::Unit messages

    aidy, Dec 13, 2007, in forum: Ruby
    Replies:
    1
    Views:
    87
    Tim Pease
    Dec 13, 2007
  5. timr
    Replies:
    2
    Views:
    167
Loading...

Share This Page