# Test::Unit assert\_raise\_s

**URL:** <https://www.ruby-forum.com/t/test-unit-assert-raise-s/85847>\
**Category:** Ruby\
**Created:** [February 16, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/test-unit-assert-raise-s/85847 "2007-02-16T19:17:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![efine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@efine](https://www.ruby-forum.com/u/efine)\
**Post date:** [February 16, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/test-unit-assert-raise-s/85847/1 "2007-02-16T19:17:32Z")

</div>

Some time ago, I complained about assert\_raise requiring one to list  
specific exceptions in a test case. My issue is that I wanted to test  
for the raising of a base exception OR any exception derived from that  
base exception, because I did not want to have to change the unit tests  
every time a different exception subclass is thrown by the method under  
test or anything it calls.

I dynamically added a new assert function to Test::Unit, named  
assert\_raise\_s, which works this way. It should not break any existing  
Test::Unit functionality. At least one person emailed me and asked me to  
provide the code for this function, so I am posting it here in case  
others might want it, too. I think that Daniel B. also talked about  
needing something like this some time ago (he wasn’t the person who  
emailed me).

In your test case file(s), you just need to

## require ‘test/unit’ require ‘assert\_raise\_s’

assert\_raise\_s.rb:

module Test  
module Unit  
module Assertions

```
  private

  def _expected_exception_or_subclass?(actual_exception, exceptions,

```

modules) # :nodoc:  
exceptions.any? {|cls| actual\_exception.class \<= cls } or  
modules.any? {|mod| actual\_exception.is\_a?(mod)}  
end

```
  ##
  # Passes if the block raises one of the given exceptions or its

```

descendants.  
#  
# Example:  
# assert\_raise\_s RuntimeError, LoadError do  
# raise ‘Boom!!!’  
# end

```
  public

  def assert_raise_s(*args)
    _wrap_assertion do
      if Module === args.last
        message = ""
      else
        message = args.pop
      end
      exceptions, modules = _check_exception_class(args)
      expected = args.size == 1 ? args.first : args
      actual_exception = nil
      full_message = build_message(message, "<?> exception expected

```

but none was thrown.", expected)  
assert\_block(full\_message) do  
begin  
yield  
rescue Exception =\> actual\_exception  
break  
end  
false  
end  
full\_message = build\_message(message, “\<?\> exception expected  
but was\n?”, expected, actual\_exception)  
assert\_block(full\_message)  
{\_expected\_exception\_or\_subclass?(actual\_exception, exceptions,  
modules)}  
actual\_exception  
end  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![efine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@efine](https://www.ruby-forum.com/u/efine)\
**Post date:** [February 16, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/test-unit-assert-raise-s/85847/2 "2007-02-16T20:15:51Z")

</div>

On Feb 16, 11:17 am, Edwin F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> provide the code for this function, so I am posting it here in case  
> others might want it, too. I think that Daniel B. also talked about  
> needing something like this some time ago (he wasn’t the person who  
> emailed me).

Yep, In my case I would like this functionality for validating system  
errors (i.e. Errno:🙂 where you don’t know in advance exactly what  
type of Errno a given method is going to raise because they can be  
platform specific.

> In your test case file(s), you just need to

Nice. Can you please re-tool this as a patch to test-unit and submit  
it to ruby-core?

Thanks,

Dan
