Exception Assertions

These methods allow you to assert the throwing of an exception.

ShouldThrowException

Asserts that the Action should throw an exception.

Usage

Where TExceptionType is an Exception
public static TExceptionType ShouldThrowException<TExceptionType>(Action action) 
public static TExceptionType ShouldThrowException<TExceptionType>(Action action, string message) 
public static TExceptionType ShouldThrowException<TExceptionType>(Action action, string message, params object[] parameters) 

Remarks

This method is contained in the Testing static class of the library.
Unlike the default Exception testing in Visual Studio, you can test multiple Exceptions within the same test method.

Example

[TestMethod]
public void Test()
{
   int zero = 0;
   Testing.ShouldThrowException<DivideByZeroException>( () => int x = 12 / zero );
}

[TestMethod]
public void Test()
{
   // Multiple expected exceptions can be tested in the same test method
   int zero = 0;
   Testing.ShouldThrowException<DivideByZeroException>( () => int x = 12 / zero );
   Testing.ShouldThrowException<InvalidCastException>( () => bool x = (bool)DateTime.Now );
}
Supported Releases: All

Last edited Dec 9, 2010 at 2:55 PM by jeremiahclark, version 3

Comments

No comments yet.