Пример #1
0
        public void GetModelBindingContext_ModelBindingContextIsNotSet_ForTypes(
            string actionMethodName, bool expectedFallToEmptyPrefix, string expectedModelName)
        {
            // Arrange
            var type       = typeof(ControllerActionArgumentBinderTests);
            var methodInfo = type.GetMethod(actionMethodName);

            var actionContext = new ActionContext(
                new RouteContext(Mock.Of <HttpContext>()),
                Mock.Of <ActionDescriptor>());

            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var modelMetadata    = metadataProvider.GetMetadataForParameter(modelAccessor: null,
                                                                            methodInfo: methodInfo,
                                                                            parameterName: "parameter1");

            // Act
            var context = DefaultControllerActionArgumentBinder.GetModelBindingContext(
                modelMetadata,
                actionContext,
                Mock.Of <OperationBindingContext>());

            // Assert
            Assert.Equal(expectedFallToEmptyPrefix, context.FallbackToEmptyPrefix);
            Assert.Equal(expectedModelName, context.ModelName);
        }
Пример #2
0
        public void GetModelBindingContext_UsesBindAttributeOnType_IfNoBindAttributeOnParameter_ForPrefix()
        {
            // Arrange
            var type          = typeof(ControllerActionArgumentBinderTests);
            var methodInfo    = type.GetMethod("ParameterWithNoBindAttribute");
            var actionContext = new ActionContext(new RouteContext(Mock.Of <HttpContext>()),
                                                  Mock.Of <ActionDescriptor>());

            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var modelMetadata    = metadataProvider.GetMetadataForParameter(modelAccessor: null,
                                                                            methodInfo: methodInfo,
                                                                            parameterName: "parameter");

            var actionBindingContext = new ActionBindingContext(actionContext,
                                                                Mock.Of <IModelMetadataProvider>(),
                                                                Mock.Of <IModelBinder>(),
                                                                Mock.Of <IValueProvider>(),
                                                                Mock.Of <IInputFormatterSelector>(),
                                                                Mock.Of <IModelValidatorProvider>());
            // Act
            var context = DefaultControllerActionArgumentBinder.GetModelBindingContext(
                modelMetadata, actionBindingContext, Mock.Of <OperationBindingContext>());

            // Assert
            Assert.Equal("TypePrefix", context.ModelName);
        }
Пример #3
0
        public void GetModelBindingContext_ReturnsOnlyIncludedProperties_UsingBindAttributeInclude()
        {
            // Arrange
            var actionContext = new ActionContext(
                new RouteContext(Mock.Of <HttpContext>()),
                Mock.Of <ActionDescriptor>());

            var metadataProvider = new DataAnnotationsModelMetadataProvider();
            var modelMetadata    = metadataProvider.GetMetadataForType(
                modelAccessor: null, modelType: typeof(TypeWithIncludedPropertiesUsingBindAttribute));

            var actionBindingContext = new ActionBindingContext();

            // Act
            var context = DefaultControllerActionArgumentBinder.GetModelBindingContext(
                modelMetadata,
                actionContext,
                Mock.Of <OperationBindingContext>());

            // Assert
            Assert.True(context.PropertyFilter(context, "IncludedExplicitly1"));
            Assert.True(context.PropertyFilter(context, "IncludedExplicitly2"));
        }