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. p>The related ThrowsNothingConstraint simply asserts that the delegate does not throw an exception.
ThrowsConstraint(Type expectedType) ThrowsConstraint<T>() ThrowsConstraint(Type expectedType, Constraint constraint) ThrowsConstraint<T>(Constraint constraint) ThrowsNothingConstraint()
Throws.Exception Throws.TargetInvocationException Throws.ArgumentException Throws.InvalidOperationException Throws.TypeOf(Type expectedType) Throws.TypeOf<T>() Throws.InstanceOf(Type expectedType) Throws.InstanceOf<T>() Throws.Nothing Throws.InnerException
// .NET 1.1 Assert.That( new TestDelegate(SomeMethod), Throws.TypeOf(typeof(ArgumentException))); Assert.That( new TestDelegate(SomeMethod), Throws.Exception.TypeOf(typeof(ArgumentException))); Assert.That( new TestDelegate(SomeMethod), Throws.TypeOf(typeof(ArgumentException)) .With.Property("Parameter").EqualTo("myParam")); Assert.That( new TestDelegate(SomeMethod), Throws.ArgumentException ); Assert.That( new TestDelegate(SomeMethod), Throws.TargetInvocationException .With.InnerException.TypeOf(ArgumentException)); // .NET 2.0 Assert.That( SomeMethod, Throws.TypeOf<ArgumentException>()); Assert.That( SomeMethod, Throws.Exception.TypeOf<ArgumentException>()); Assert.That( SomeMethod, Throws.TypeOf<ArgumentException>() .With.Property("Parameter").EqualTo("myParam")); Assert.That( SomeMethod, Throws.ArgumentException ); Assert.That( SomeMethod, Throws.TargetInvocationException .With.InnerException.TypeOf<ArgumentException>());