Forum: Ruby Test::Unit assert_throws

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.
75b6377ab4f763c411acd4a3815f7dbb?d=identicon&s=25 Mike Burrows (asplake)
on 2006-01-01 18:45
(Received via mailing list)
Hi,

Any good examples out there (I couldn't find any) of assert_throws?  I
would love to know also why its first argument is a symbol...

Thanks
Mike
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-01 18:51
(Received via mailing list)
On Jan 1, 2006, at 11:42 AM, asplake wrote:

> Hi,
>
> Any good examples out there (I couldn't find any) of assert_throws?  I
> would love to know also why its first argument is a symbol...

I'm guessing you might be looking for Exception handling assertions.
If so, you want:

assert_raised()
assert_nothing_raised()

assert_throws() is related to catch()/throw(), useful in Ruby for
breaking out of nested constructs.

I hope that helps.

James Edward Gray II
851246810c70dbfcc1815c636b054562?d=identicon&s=25 George Ogata (Guest)
on 2006-01-02 08:35
(Received via mailing list)
"asplake" <mjb@asplake.co.uk> writes:

> Hi,
>
> Any good examples out there (I couldn't find any) of assert_throws?  I
> would love to know also why its first argument is a symbol...

#assert_throw checks for a Kernel#throw, which takes a Symbol.
Perhaps you were after #assert_raise ?

--------------------

require 'test/unit'

def f
  throw :x
end

def g
  raise
end

class T < Test::Unit::TestCase
  def test_a
    assert_throws(:x){f}  # pass
    assert_throws(:y){f}  # fail
  end
  def test_b
    assert_raise(RuntimeError ){g}  # pass
    assert_raise(ArgumentError){g}  # fail
  end
end
This topic is locked and can not be replied to.