Beispiel #1
0
        /// <summary>
        /// Tests whether model of the given type is returned from the invoked method.
        /// </summary>
        /// <param name="builder">Instance of <see cref="IBaseTestBuilderWithResponseModel"/> type.</param>
        /// <typeparam name="TModel">Type of the model.</typeparam>
        /// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TModel}"/>.</returns>
        public static IAndModelDetailsTestBuilder <TModel> WithModelOfType <TModel>(
            this IBaseTestBuilderWithResponseModel builder)
        {
            var actualBuilder = (BaseTestBuilderWithResponseModel)builder;

            var actualModelType   = actualBuilder.GetModelReturnType();
            var expectedModelType = typeof(TModel);

            var modelIsAssignable = Reflection.AreAssignable(
                expectedModelType,
                actualModelType);

            if (!modelIsAssignable)
            {
                var(expectedModelName, actualModelName) = (expectedModelType, actualModelType).GetTypeComparisonNames();

                throw new ResponseModelAssertionException(string.Format(
                                                              actualBuilder.OfTypeErrorMessageFormat,
                                                              actualBuilder.TestContext.ExceptionMessagePrefix,
                                                              expectedModelName,
                                                              actualModelName));
            }

            actualBuilder.TestContext.Model = actualBuilder.GetActualModel <TModel>();

            return(new ModelDetailsTestBuilder <TModel>(actualBuilder.TestContext));
        }
 public object TryGetValue(Type type, ComponentTestContext testContext)
 => Reflection.AreAssignable(this.baseHttpContextType, type)         // HttpContext
         ? testContext.HttpContext
         : Reflection.AreAssignable(this.baseHttpRequestType, type)  // HttpRequest
         ? testContext.HttpRequest
         : Reflection.AreAssignable(this.baseHttpResponseType, type) // HttpResponse
         ? (object)testContext.HttpResponse
         : null;
Beispiel #3
0
        public void AreAssignableShouldReturnTrueWithInvertedInheritedTypes()
        {
            var baseType      = typeof(IList <int>);
            var inheritedType = typeof(IEnumerable <int>);

            Assert.False(Reflection.AreAssignable(baseType, inheritedType));
            Assert.True(Reflection.AreNotAssignable(baseType, inheritedType));
        }
Beispiel #4
0
        public void AreAssignableShouldReturnFalseWithOneGenericTypeDefinition()
        {
            var baseType      = typeof(IEnumerable <>);
            var inheritedType = typeof(IList <int>);

            Assert.False(Reflection.AreAssignable(baseType, inheritedType));
            Assert.True(Reflection.AreNotAssignable(baseType, inheritedType));
        }
Beispiel #5
0
        public void AreAssignableShouldReturnTrueWithTheSameTypes()
        {
            var first  = typeof(int);
            var second = typeof(int);

            Assert.True(Reflection.AreAssignable(first, second));
            Assert.False(Reflection.AreNotAssignable(first, second));
        }
        private static IDictionary <string, TValue> ObjectToDictionary <TValue>(object obj)
        {
            if (obj is IDictionary <string, TValue> objAsStringValueDictionary)
            {
                return(objAsStringValueDictionary);
            }

            var result      = new RouteValueDictionary(obj).AsEnumerable();
            var typeOfValue = typeof(TValue);

            if (typeOfValue != typeof(object))
            {
                result = result.Where(i => Reflection.AreAssignable(typeof(TValue), i.Value?.GetType()));
            }

            return(result.ToDictionary(i => i.Key.Replace('_', '-'), i => (TValue)i.Value));
        }
        /// <inheritdoc />
        public IAndModelDetailsTestBuilder <TModel> WithModelOfType <TModel>()
        {
            var actualResponseDataType   = this.GetReturnType();
            var expectedResponseDataType = typeof(TModel);

            var responseDataTypeIsAssignable = Reflection.AreAssignable(
                expectedResponseDataType,
                actualResponseDataType);

            if (!responseDataTypeIsAssignable)
            {
                throw new ResponseModelAssertionException(string.Format(
                                                              this.OfTypeErrorMessageFormat,
                                                              this.TestContext.ExceptionMessagePrefix,
                                                              typeof(TModel).ToFriendlyTypeName(),
                                                              actualResponseDataType.ToFriendlyTypeName()));
            }

            this.TestContext.Model = this.GetActualModel <TModel>();
            return(new ModelDetailsTestBuilder <TModel>(this.TestContext));
        }
Beispiel #8
0
        /// <summary>
        /// Tests whether certain type of response model is returned from the invoked action.
        /// </summary>
        /// <typeparam name="TResponseModel">Type of the response model.</typeparam>
        /// <returns>Builder for testing the response model errors.</returns>
        public IModelDetailsTestBuilder <TResponseModel> WithResponseModelOfType <TResponseModel>()
        {
            var actionResultType            = this.ActionResult.GetType();
            var negotiatedContentResultType = typeof(OkNegotiatedContentResult <TResponseModel>);

            var negotiatedActionResultIsAssignable = Reflection.AreAssignable(
                actionResultType,
                negotiatedContentResultType);

            if (!negotiatedActionResultIsAssignable)
            {
                if (actionResultType.IsGenericType)
                {
                    var actualResponseDataType   = actionResultType.GetGenericArguments()[0];
                    var expectedResponseDataType = typeof(TResponseModel);

                    var responseDataTypeIsAssignable = Reflection.AreAssignable(
                        expectedResponseDataType,
                        actualResponseDataType);

                    if (!responseDataTypeIsAssignable)
                    {
                        throw new ResponseModelAssertionException(string.Format(
                                                                      "When calling {0} action in {1} expected response model to be a {2}, but instead received a {3}.",
                                                                      this.ActionName,
                                                                      this.Controller.GetName(),
                                                                      typeof(TResponseModel).ToFriendlyTypeName(),
                                                                      actualResponseDataType.ToFriendlyTypeName()));
                    }
                }
            }

            return(new ModelDetailsTestBuilder <TResponseModel>(
                       this.Controller,
                       this.ActionName,
                       this.CaughtException,
                       this.GetActualModel <TResponseModel>()));
        }
Beispiel #9
0
 public object TryGetValue(Type type, ComponentTestContext testContext)
 => Reflection.AreAssignable(this.controllerAttributesType, type)    // ControllerAttributes
         ? new ControllerAttributes(testContext.ComponentAttributes)
         : Reflection.AreAssignable(this.actionAttributesType, type) // ActionAttributes
         ? (object)new ActionAttributes(testContext.MethodAttributes)
         : null;
 public object TryGetValue(Type type, ComponentTestContext testContext)
 => Reflection.AreAssignable(viewComponentAttributesType, type)     // ViewComponentAttributes
         ? new ViewComponentAttributes(testContext.ComponentAttributes)
         : null;
 public object TryGetValue(Type type, ComponentTestContext testContext)
 => Reflection.AreAssignable(this.exceptionType, type)     // Exception.
         ? testContext.CaughtException
         : null;
 public object TryGetValue(Type type, ComponentTestContext testContext)
 => Reflection.AreAssignable(this.controllerAttributesType, type)    // ControllerAttributes
         ? new ControllerAttributes(testContext.ComponentAttributes)
         : Reflection.AreAssignable(this.actionAttributesType, type) // ActionAttributes
         ? new ActionAttributes(testContext.MethodAttributes)
         : testContext.MethodResult is ObjectResult objectResult && // ObjectResult Model
Beispiel #13
0
 public object TryGetValue(Type type, ComponentTestContext testContext)
 => Reflection.AreAssignable(serviceProviderType, type)     // ServiceProvider
         ? testContext.HttpContext.RequestServices
         : null;