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());
            }
        }
 public void CheckForExceptionShouldThrowIfExceptionNotNullWithMessage()
 {
     Test.AssertException <ActionCallAssertionException>(
         () =>
     {
         CommonValidator.CheckForException(new NullReferenceException("Test"));
     },
         "NullReferenceException with 'Test' message was thrown but was not caught or expected.");
 }
Beispiel #3
0
        public void CheckForExceptionShouldThrowWithProperMessageIfExceptionIsAggregateException()
        {
            var aggregateException = new AggregateException(new List <Exception>
            {
                new NullReferenceException("Null test"),
                new InvalidCastException("Cast test"),
                new InvalidOperationException("Operation test")
            });

            CommonValidator.CheckForException(aggregateException);
        }
        public void CheckForExceptionShouldThrowWithProperMessageIfExceptionIsAggregateException()
        {
            var aggregateException = new AggregateException(new List <Exception>
            {
                new NullReferenceException("Null test"),
                new InvalidCastException("Cast test"),
                new InvalidOperationException("Operation test")
            });

            Test.AssertException <ActionCallAssertionException>(
                () =>
            {
                CommonValidator.CheckForException(aggregateException);
            },
                "AggregateException (containing NullReferenceException with 'Null test' message, InvalidCastException with 'Cast test' message, InvalidOperationException with 'Operation test' message) was thrown but was not caught or expected.");
        }
Beispiel #5
0
        /// <summary>
        /// Testuje handler HTTP na úspešné vrátenie správy HTTP.
        /// </summary>
        /// <returns>HTTP response message test builder.</returns>
        public IHttpHandlerResponseMessageTestBuilder ShouldReturnHttpResponseMessage()
        {
            HttpResponseMessage httpResponseMessage = null;

            using (var httpMessageInvoker = new HttpMessageInvoker(this.Handler))
            {
                try
                {
                    httpResponseMessage = httpMessageInvoker.SendAsync(this.httpRequestMessage, CancellationToken.None).Result;
                }
                catch (Exception exception)
                {
                    CommonValidator.CheckForException(exception);
                }
            }

            return(new HttpHandlerResponseMessageTestBuilder(
                       httpResponseMessage));
        }
 /// <summary>
 /// Used for testing returned action result.
 /// </summary>
 /// <returns>Should return test builder.</returns>
 public IShouldReturnTestBuilder <TActionResult> ShouldReturn()
 {
     CommonValidator.CheckForException(this.CaughtException);
     return(new ShouldReturnTestBuilder <TActionResult>(this.Controller, this.ActionName, this.CaughtException, this.ActionResult));
 }
Beispiel #7
0
 /// <inheritdoc />
 public IShouldReturnTestBuilder <TActionResult> ShouldReturn()
 {
     TestHelper.ExecuteTestCleanup();
     CommonValidator.CheckForException(this.CaughtException);
     return(new ShouldReturnTestBuilder <TActionResult>(this.TestContext));
 }
Beispiel #8
0
 /// <inheritdoc />
 public IShouldHaveTestBuilder <TActionResult> ShouldHave()
 {
     CommonValidator.CheckForException(this.CaughtException);
     return(new ShouldHaveTestBuilder <TActionResult>(this.TestContext));
 }
 /// <inheritdoc />
 public IBaseTestBuilderWithInvokedAction ShouldReturnEmpty()
 {
     CommonValidator.CheckForException(this.CaughtException);
     return(this.NewAndProvideTestBuilder());
 }
 public void CheckForExceptionShouldNotThrowIfExceptionNull()
 {
     CommonValidator.CheckForException(null);
 }
Beispiel #11
0
 public void CheckForExceptionShouldThrowIfExceptionIsNotNullWithMessage()
 {
     CommonValidator.CheckForException(new NullReferenceException("Test"));
 }
Beispiel #12
0
 public void CheckForExceptionShouldThrowIfExceptionIsNotNullWithEmptyMessage()
 {
     CommonValidator.CheckForException(new NullReferenceException(string.Empty));
 }