Throws Constraint (NUnit 2.5)

ThrowsConstraint is used to test that some code, represented as a delegate, throws a particular exception. It may be used alone, to merely test the type of constraint, or with an additional constraint to be applied to the exception specified as an argument.

Syntax HelperConstructorOperation
Throws.Exception(Type)ThrowsConstraint(Type)tests that an exception of the specified type is thrown
Throws.Exception(Type,Constraint)ThrowsConstraint(Type,Constraint)tests that an exception of the specified type is thrown and that it satisifies the constrant provided as the second argument
Throws.Exception<T>()ThrowsConstraint<T>()tests that an exception of the specified type is thrown
Throws.Exception<T>(Constraint)ThrowsConstraint<T>(Constraint)tests that an exception of the specified type is thrown and that it satisifies the constrant provided as an argument

Examples


// .NET 1.1
Assert.That( new TestDelegate(SomeMethod),
  Throws.Exception(typeof(ArgumentException)));
Assert.That( new TestDelegate(SomeMethod),
  Throws.Exception(typeof(ArgumentException),
    Has.Property("Parameter", "myParam")));
	
// .NET 2.0
Assert.That( SomeMethod, 
  Throws.Exception<ArgumentException>());
Assert.That( SomeMethod, 
  Throws.Exception<ArgumentException>(
    Has.Property("Parameter", "myParam")));