private void ValidateActionReturnType(Type typeOfExpectedReturnValue, bool canBeAssignable = false, bool allowDifferentGenericTypeDefinitions = false) { CommonValidator.CheckForException(this.CaughtException); var typeInfoOfExpectedReturnValue = typeOfExpectedReturnValue.GetTypeInfo(); var typeOfActionResult = this.ActionResult?.GetType(); if (typeOfActionResult == null) { this.ThrowNewGenericActionResultAssertionException( typeOfExpectedReturnValue.ToFriendlyTypeName(), "null"); } var typeInfoOfActionResult = typeOfActionResult.GetTypeInfo(); var isAssignableCheck = canBeAssignable && Reflection.AreNotAssignable(typeOfExpectedReturnValue, typeOfActionResult); if (isAssignableCheck && allowDifferentGenericTypeDefinitions && Reflection.IsGeneric(typeOfExpectedReturnValue) && Reflection.IsGenericTypeDefinition(typeOfExpectedReturnValue)) { isAssignableCheck = Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); if (!isAssignableCheck && !typeInfoOfActionResult.IsGenericType) { isAssignableCheck = true; } else { isAssignableCheck = !Reflection.ContainsGenericTypeDefinitionInterface(typeOfExpectedReturnValue, typeOfActionResult); } } var strictlyEqualCheck = !canBeAssignable && Reflection.AreDifferentTypes(typeOfExpectedReturnValue, typeOfActionResult); var invalid = isAssignableCheck || strictlyEqualCheck; if (invalid && typeInfoOfExpectedReturnValue.IsGenericTypeDefinition && typeInfoOfActionResult.IsGenericType) { var actionResultGenericDefinition = typeInfoOfActionResult.GetGenericTypeDefinition(); if (actionResultGenericDefinition == typeOfExpectedReturnValue) { invalid = false; } } if (invalid && typeInfoOfExpectedReturnValue.IsGenericType && typeInfoOfActionResult.IsGenericType) { invalid = !Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); } if (invalid) { this.ThrowNewGenericActionResultAssertionException( typeOfExpectedReturnValue.ToFriendlyTypeName(), typeOfActionResult.ToFriendlyTypeName()); } }
private void ValidateActionReturnType(params Type[] returnTypes) { var typeOfActionResult = this.ActionResult.GetType(); if (returnTypes.All(t => !Reflection.AreAssignableByGeneric(t, typeOfActionResult))) { this.ThrowNewGenericHttpActionResultAssertionException( string.Join(" or ", returnTypes.Select(t => t.ToFriendlyTypeName())), typeOfActionResult.ToFriendlyTypeName()); } }
public static ActionTestContext ConvertMethodResult(this ActionTestContext testContext) { var methodReturnType = testContext.Method.ReturnType; if (Reflection.AreAssignableByGeneric(ActionResultGenericType, methodReturnType)) { var methodResultType = testContext.MethodResult.GetType(); if (Reflection.AreSameTypes(ObjectResultType, methodResultType)) { var objectResult = testContext.MethodResult as ObjectResult; testContext.MethodResult = objectResult?.Value; } } return(testContext); }
public static void ValidateInvocationResultType( ComponentTestContext testContext, Type typeOfExpectedReturnValue, bool canBeAssignable = false, bool allowDifferentGenericTypeDefinitions = false) { InvocationValidator.CheckForException(testContext.CaughtException, testContext.ExceptionMessagePrefix); var typeInfoOfExpectedReturnValue = typeOfExpectedReturnValue.GetTypeInfo(); var typeOfActionResult = testContext.MethodResult?.GetType(); if (typeOfActionResult == null) { ThrowNewInvocationResultAssertionException( testContext, typeOfExpectedReturnValue.ToFriendlyTypeName(), "null"); } var typeInfoOfActionResult = typeOfActionResult.GetTypeInfo(); var isAssignableCheck = canBeAssignable && Reflection.AreNotAssignable(typeOfExpectedReturnValue, typeOfActionResult); if (isAssignableCheck && allowDifferentGenericTypeDefinitions && Reflection.IsGeneric(typeOfExpectedReturnValue) && Reflection.IsGenericTypeDefinition(typeOfExpectedReturnValue)) { isAssignableCheck = Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); if (!isAssignableCheck && !typeInfoOfActionResult.IsGenericType) { isAssignableCheck = true; } else { isAssignableCheck = !Reflection.ContainsGenericTypeDefinitionInterface(typeOfExpectedReturnValue, typeOfActionResult); } } var strictlyEqualCheck = !canBeAssignable && Reflection.AreDifferentTypes(typeOfExpectedReturnValue, typeOfActionResult); var invalid = isAssignableCheck || strictlyEqualCheck; if (invalid && typeInfoOfExpectedReturnValue.IsGenericTypeDefinition && typeInfoOfActionResult.IsGenericType) { var actionResultGenericDefinition = typeInfoOfActionResult.GetGenericTypeDefinition(); if (actionResultGenericDefinition == typeOfExpectedReturnValue) { invalid = false; } } if (invalid && typeInfoOfExpectedReturnValue.IsGenericType && typeInfoOfActionResult.IsGenericType) { invalid = !Reflection.AreAssignableByGeneric(typeOfExpectedReturnValue, typeOfActionResult); } if (invalid) { ThrowNewInvocationResultAssertionException( testContext, typeOfExpectedReturnValue.ToFriendlyTypeName(), typeOfActionResult.ToFriendlyTypeName()); } }