using System;
using System.Reflection;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding.Test
{
public static class ExceptionAssert
{
///
/// Verifies that an exception of the given type (or optionally a derived type) is thrown.
///
/// The type of the exception expected to be thrown
/// A delegate to the code to be tested
/// The exception that was thrown, when successful
/// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown
public static TException Throws(Action testCode)
where TException : Exception
{
Type exceptionType = typeof(TException);
Exception exception = RecordException(testCode);
TargetInvocationException tie = exception as TargetInvocationException;
if (tie != null)
{
exception = tie.InnerException;
}
Assert.NotNull(exception);
return Assert.IsAssignableFrom(exception);
}
///
/// Verifies that an exception of the given type (or optionally a derived type) is thrown.
/// Also verifies that the exception message matches.
///
/// The type of the exception expected to be thrown
/// A delegate to the code to be tested
/// The exception message to verify
/// The exception that was thrown, when successful
/// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown
public static TException Throws(Action testCode, string exceptionMessage)
where TException : Exception
{
var ex = Throws(testCode);
VerifyExceptionMessage(ex, exceptionMessage);
return ex;
}
///
/// Verifies that an exception of the given type (or optionally a derived type) is thrown.
/// Also verified that the exception message matches.
///
/// The type of the exception expected to be thrown
/// A delegate to the code to be tested
/// The exception message to verify
/// The exception that was thrown, when successful
/// Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown
public static TException Throws(Func