Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I get NUnit's ExpectedException attribute to detect an exception's base class?

Using NUnit 2.5.10, I am testing some code that references a library containing a base exception type. TIBCO.EMS.NamingException, from which other exception types derive, specifically TIBCO.EMS.InvalidNameException and TIBCO.EMS.NameNotFoundException.

I would like to use NUnit's ExpectedException attribute to recognize when any subclassed exception deriving from TIBCO.EMS.NamingException has been thrown.

I can easily detect when the specific exception has been thrown:

[ExpectedException("TIBCO.EMS.NameNotFoundException")]
       or 
[ExpectedException(Typeof(TIBCO.EMS.InvalidNameException))]

But I would like to somehow make NUnit "expect" whether any subclass of TIBCO.EMS.NamingException has been thrown.

Trying it directly does not work:

[ExpectedException("TIBCO.EMS.NamingException")]
    or
[ExpectedException(typeof(TIBCO.EMS.NamingException))]

Any ideas?

like image 751
John Tobler Avatar asked Aug 16 '11 22:08

John Tobler


1 Answers

From NUnit documentation:

// Allow both ApplicationException and any derived type
Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
Assert.Throws( Is.InstanceOf<ApplicationException>(), code );
like image 165
sll Avatar answered Nov 14 '22 01:11

sll