public void BindModel()
        {
            // Arrange
            Mock <IValueProvider> mockValueProvider = new Mock <IValueProvider>();

            mockValueProvider.Setup(o => o.ContainsPrefix(It.IsAny <string>())).Returns(true);

            Mock <IModelBinder> mockDtoBinder  = new Mock <IModelBinder>();
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = GetMetadataForObject(new Person()),
                ModelName     = "someName",
                ValueProvider = mockValueProvider.Object
            };
            HttpActionContext context = ContextUtil.CreateActionContext();

            context.ControllerContext.Configuration.Services.Replace(
                typeof(ModelBinderProvider),
                new SimpleModelBinderProvider(typeof(ComplexModelDto), mockDtoBinder.Object)
            {
                SuppressPrefixCheck = true
            }
                );

            mockDtoBinder
            .Setup(o => o.BindModel(context, It.IsAny <ModelBindingContext>()))
            .Returns(
                (HttpActionContext cc, ModelBindingContext mbc2) =>
            {
                return(true);        // just return the DTO unchanged
            }
                );

            Mock <TestableMutableObjectModelBinder> mockTestableBinder =
                new Mock <TestableMutableObjectModelBinder> {
                CallBase = true
            };

            mockTestableBinder
            .Setup(o => o.EnsureModelPublic(context, bindingContext))
            .Verifiable();
            mockTestableBinder
            .Setup(o => o.GetMetadataForPropertiesPublic(context, bindingContext))
            .Returns(new ModelMetadata[0])
            .Verifiable();
            TestableMutableObjectModelBinder testableBinder = mockTestableBinder.Object;

            testableBinder.MetadataProvider = new DataAnnotationsModelMetadataProvider();

            // Act
            bool retValue = testableBinder.BindModel(context, bindingContext);

            // Assert
            Assert.True(retValue);
            Assert.IsType <Person>(bindingContext.Model);
            Assert.True(bindingContext.ValidationNode.ValidateAllProperties);
            mockTestableBinder.Verify();
        }
Exemple #2
0
        public void BindModel()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext
            {
                ModelBinderProviders = new ModelBinderProviderCollection(),
                ModelMetadata        = GetMetadataForObject(new Person()),
                ModelName            = "someName"
            };

            Mock <IExtensibleModelBinder> mockDtoBinder = new Mock <IExtensibleModelBinder>();

            mockDtoBinder
            .Setup(
                o => o.BindModel(controllerContext, It.IsAny <ExtensibleModelBindingContext>())
                )
            .Returns(
                delegate(ControllerContext cc, ExtensibleModelBindingContext mbc2)
            {
                return(true);        // just return the DTO unchanged
            }
                );
            bindingContext.ModelBinderProviders.RegisterBinderForType(
                typeof(ComplexModelDto),
                mockDtoBinder.Object,
                true /* suppressPrefixCheck */
                );

            Mock <TestableMutableObjectModelBinder> mockTestableBinder =
                new Mock <TestableMutableObjectModelBinder> {
                CallBase = true
            };

            mockTestableBinder
            .Setup(o => o.EnsureModelPublic(controllerContext, bindingContext))
            .Verifiable();
            mockTestableBinder
            .Setup(o => o.GetMetadataForPropertiesPublic(controllerContext, bindingContext))
            .Returns(new ModelMetadata[0])
            .Verifiable();
            TestableMutableObjectModelBinder testableBinder = mockTestableBinder.Object;

            testableBinder.MetadataProvider = new DataAnnotationsModelMetadataProvider();

            // Act
            bool retValue = testableBinder.BindModel(controllerContext, bindingContext);

            // Assert
            Assert.True(retValue);
            Assert.IsType <Person>(bindingContext.Model);
            Assert.True(bindingContext.ValidationNode.ValidateAllProperties);
            mockTestableBinder.Verify();
        }