How can I get the NUnit ExpectedException attribute to determine the base class of the exception?

Using NUnit 2.5.10 , I am testing code that references a library containing the base type of an exception. TIBCO.EMS.NamingException from which other types of exceptions are thrown, in particular TIBCO.EMS.InvalidNameException and TIBCO.EMS.NameNotFoundException .

I would like to use the NUnit ExpectedException attribute to recognize when any subclass exception was thrown from TIBCO.EMS.NamingException .

I can easily detect when a particular exception was thrown:

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

But I would like to somehow make NUnit "wait" if any subclass of TIBCO.EMS.NamingException .

Trying it directly does not work:

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

Any ideas?

+6
source share
1 answer

From the NUnit Documentation :

 // Allow both ApplicationException and any derived type Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code ); Assert.Throws( Is.InstanceOf<ApplicationException>(), code ); 
+9
source

Source: https://habr.com/ru/post/895201/


All Articles