6

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?

sll
  • 56,967
  • 21
  • 100
  • 149
John Tobler
  • 404
  • 2
  • 14

1 Answers1

9

From NUnit documentation:

// Allow both ApplicationException and any derived type
Assert.Throws( Is.InstanceOf( typeof(ApplicationException), code );
Assert.Throws( Is.InstanceOf<ApplicationException>(), code );
sll
  • 56,967
  • 21
  • 100
  • 149
  • 2
    That worked perfectly! I was so enamored of the ExpectedException attribute, which I have used so helpfully so many times before, that I missed going back to a different part of the documentation. I was trying to make a glove do the duty of a boot! Thank you, @silev for teaching me a useful new trick! – John Tobler Aug 16 '11 at 23:47
  • 1
    Great, you are welcome! Also I like generic version Assert.Throws() and very useful Assert.DoesNotThrow(() => { ... }) – sll Aug 16 '11 at 23:50