/// <summary> /// Verifica che il tipo dell'eccezione generata dallo unit test sia prevista /// </summary> /// <param name="exception">Eccezione generata dallo unit test</param> protected override void Verify(Exception exception) { Type type = ((object)exception).GetType(); // Getting exception's code string actualExceptionCode = (string)exception.GetType().GetProperty("Code").GetValue(exception); bool isWrongExceptionType; // Verifying ExceptionType if (AllowDerivedTypes) { isWrongExceptionType = !ExceptionType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()); } else { isWrongExceptionType = (object)type != ExceptionType; } // Verifying Exception code and throwing exception in one of the verifications fails if (isWrongExceptionType || !actualExceptionCode.Equals(ExceptionCode)) { RethrowIfAssertException(exception); throw new Exception(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.UTF_TestMethodWrongException, new object[3] { type.FullName, ExceptionType.FullName, ExceptionUtils.GetExceptionMsg(exception) })); } }
public ClaimsPrincipal ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken) { var constructor = ExceptionType.GetTypeInfo().GetConstructor(new[] { typeof(string) }); var exception = (Exception)constructor.Invoke(new[] { ExceptionType.Name }); throw exception; }
/// <summary> /// Verifies that the type of the exception thrown by the unit test is expected /// </summary> /// <param name="exception">The exception thrown by the unit test</param> protected override void Verify(Exception exception) { if (exception.GetType() == typeof(AggregateException)) { exception = exception.InnerException; } Type type = ((object)exception).GetType(); if (AllowDerivedTypes) { if (!ExceptionType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) { RethrowIfAssertException(exception); throw new Exception($"Test method threw exception {type.FullName}, but exception {ExceptionType.FullName} or a type derived from it was expected. Exception message: {GetExceptionMsg(exception)}"); } } else if ((object)type != ExceptionType) { RethrowIfAssertException(exception); throw new Exception($"Test method threw exception {type.FullName}, but exception {ExceptionType.FullName} was expected. Exception message: {GetExceptionMsg(exception)}"); } }