Forum: Ruby Asserting exception messages?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2007-02-16 20:36
(Received via mailing list)
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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-02-16 22:18
(Received via mailing list)
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
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2007-02-17 15:21
(Received via mailing list)
On Sat, 2007-02-17 at 06:17 +0900, Eric Hodel 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!
This topic is locked and can not be replied to.