/// <summary> /// Asserts that the given action throws the specified exception type. /// </summary> /// <param name="action">Action to try.</param> /// <param name="expectedException">The expected exception.</param> /// <param name="exceptionVerifier">The exception verifier.</param> public static void ExpectedException(this AssertionHandler assert, Action action, ExpectedException expectedException, IExceptionVerifier exceptionVerifier) { ExceptionUtilities.CheckArgumentNotNull(assert, "assert"); ExceptionUtilities.CheckArgumentNotNull(action, "action"); ExceptionUtilities.CheckArgumentNotNull(exceptionVerifier, "exceptionVerifier"); Exception exception = RunCatching(action); if (exception == null && expectedException == null) { return; } else if (exception == null) { assert.IsNotNull(exception, "Expected exception of type '{0}' with message resource ID '{1}' but none was thrown.", expectedException.ExpectedExceptionType.ToString(), expectedException.ExpectedMessage == null ? "<null>" : expectedException.ExpectedMessage.ResourceIdentifier); } else if (expectedException == null) { assert.IsNotNull(expectedException, "Did not expect an exception but an exception of type '{0}' with message '{1}' was thrown.", exception.GetType().ToString(), exception.Message); } exception = TestExceptionUtils.UnwrapAggregateException(exception, assert); exceptionVerifier.VerifyExceptionResult(expectedException, exception); }
/// <summary> /// Asserts that the given action throws the specified exception type. /// </summary> /// <typeparam name="TException">Exception type.</typeparam> /// <param name="action">Action to try.</param> /// <param name="expectedExceptionMessage">The expected exception message.</param> /// <param name="desciption">String to attach to all errors so that it's easier to locate what went wrong.</param> public static void ExpectedException <TException>(this AssertionHandler assert, Action action, string expectedExceptionMessage, string description = null) { Exception exception = RunCatching(action); exception = TestExceptionUtils.UnwrapAggregateException(exception, assert); IsExpectedException <TException>(assert, exception, expectedExceptionMessage, description); }