# Generic way to test for expected SystemCallError

**URL:** <https://www.ruby-forum.com/t/generic-way-to-test-for-expected-systemcallerror/77162>\
**Category:** Ruby\
**Created:** [November 24, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/generic-way-to-test-for-expected-systemcallerror/77162 "2006-11-24T21:37:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [November 24, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/generic-way-to-test-for-expected-systemcallerror/77162/1 "2006-11-24T21:37:00Z")

</div>

Hi all,

Ruby 1.8.x

I’ve got a few tests where I’m looking for expected errors. These are  
various system call errors. The problem is that the exact Errno is  
different from platform to platform, so I end up writing stuff like  
this:

assert\_raises(Errno::EACCES, Errno::ENOTEMPTY){ Dir.unlink(‘some\_dir’)  
}

This isn’t very robust, since a new platform might fail with a  
different kind of Errno. What I would like is a way to have this test  
succeed:

assert\_raises(SystemCallError){ Dir.unlink(‘some\_dir’) }

The problem is that assert\_raises looks for a specific error class. Is  
there a way to tell TestUnit to expect any error that’s a subclass of  
SystemCallError?

Thanks,

Dan

PS - I see from the documentation that assert\_raise is deprecated in  
1.9. What’s replacing it?

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [November 24, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/generic-way-to-test-for-expected-systemcallerror/77162/2 "2006-11-24T22:37:23Z")

</div>

Daniel B. wrote:

> }
> 
> Thanks,
> 
> Dan
> 
> PS - I see from the documentation that assert\_raise is deprecated in  
> 1.9. What’s replacing it?

The simplest way to do this which comes to my mind is to use the fact  
that the  
exceptions you need are the only constants in the Errno module. This  
allows  
you to write

assert\_raise(\*(Errno.constants.map{|c| Errno.const\_get©}))  
{Dir.unlink(‘some\_dir’)}

Errno.constants gives you an array with the names of the constants,  
which are  
the names of the exception claesses you’re interested in. The map method  
transforms the array of names in an array of classes, obtained using  
Errno.const\_get

As for assert\_raises being deprecated, I think it’s simply a matter of  
names,  
since the method replacing it is simply assert\_raise (without the final  
s).

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [November 25, 2006, 1:31am UTC](https://www.ruby-forum.com/t/generic-way-to-test-for-expected-systemcallerror/77162/3 "2006-11-25T01:31:49Z")

</div>

Stefano C. wrote:

> > assert\_raises(Errno::EACCES, Errno::ENOTEMPTY){ Dir.unlink(‘some\_dir’)  
> > SystemCallError?  
> > you to write
> 
> assert\_raise(\*(Errno.constants.map{|c| Errno.const\_get©}))  
> {Dir.unlink(‘some\_dir’)}
> 
> Errno.constants gives you an array with the names of the constants, which are  
> the names of the exception claesses you’re interested in. The map method  
> transforms the array of names in an array of classes, obtained using  
> Errno.const\_get

Ugly, but I guess it will have to do. I’ll probably wrap that in its  
own method. Perhaps I can convince Nathaniel to either add a  
“assert\_raise\_kind\_of” method, or allow assert\_raise to take another  
argument.

> As for assert\_raises being deprecated, I think it’s simply a matter of names,  
> since the method replacing it is simply assert\_raise (without the final s).

Oh, I could have sworn they were both deprecated. Perhaps I was  
looking at old docs. Disregard. I can’t wait to go through all of my  
tests and update those. Not.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [November 25, 2006, 7:09am UTC](https://www.ruby-forum.com/t/generic-way-to-test-for-expected-systemcallerror/77162/4 "2006-11-25T07:09:38Z")

</div>

I had the same issue with TestUnit. I wound up writing my own  
assert\_raise function, which I named assert\_raise\_s, based on the  
TestUnit assert\_raise code. I then extend test/unit with the code.

require ‘test/unit’  
require ‘assert\_raise\_s.rb’

I tried to attach the code but am getting a Rails Application Error.  
Should I post the code itself?
