예제 #1
0
        public void GetBinderReturnsFirstBinderFromProviders()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object))
            };
            IExtensibleModelBinder expectedBinder = new Mock <IExtensibleModelBinder>().Object;

            Mock <ModelBinderProvider> mockProvider = new Mock <ModelBinderProvider>();

            mockProvider.Setup(p => p.GetBinder(controllerContext, bindingContext)).Returns(expectedBinder);

            ModelBinderProviderCollection collection = new ModelBinderProviderCollection(new ModelBinderProvider[] {
                new Mock <ModelBinderProvider>().Object,
                mockProvider.Object,
                new Mock <ModelBinderProvider>().Object
            });

            // Act
            IExtensibleModelBinder returned = collection.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(expectedBinder, returned);
        }
예제 #2
0
        public void GetBinderThrowsIfControllerContextIsNull()
        {
            // Arrange
            ModelBinderProviderCollection collection = new ModelBinderProviderCollection();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                collection.GetBinder(null, new ExtensibleModelBindingContext());
            }, "controllerContext");
        }
예제 #3
0
        public void GetBinderThrowsIfModelTypeHasBindAttribute()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithBindAttribute))
            };
            ModelBinderProviderCollection collection = new ModelBinderProviderCollection();

            // Act & assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                collection.GetBinder(controllerContext, bindingContext);
            },
                @"The model of type 'Microsoft.Web.Mvc.ModelBinding.Test.ModelBinderProviderCollectionTest+ModelWithBindAttribute' has a [Bind] attribute. The new model binding system cannot be used with models that have type-level [Bind] attributes. Use the [BindRequired] and [BindNever] attributes on the model type or its properties instead.");
        }
예제 #4
0
        public void GetBinder_FromAttribute_BadAttribute_Throws()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithProviderAttribute_BadAttribute))
            };

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection();

            // Act & assert
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                providers.GetBinder(controllerContext, bindingContext);
            },
                @"The type 'System.Object' does not subclass Microsoft.Web.Mvc.ModelBinding.ModelBinderProvider or implement the interface Microsoft.Web.Mvc.ModelBinding.IExtensibleModelBinder.");
        }
예제 #5
0
        public void GetBinderReturnsNullIfNoProviderMatches()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(object))
            };

            ModelBinderProviderCollection collection = new ModelBinderProviderCollection(new ModelBinderProvider[] {
                new Mock <ModelBinderProvider>().Object,
            });

            // Act
            IExtensibleModelBinder returned = collection.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.IsNull(returned);
        }
예제 #6
0
        public void GetBinder_FromAttribute_Provider_ReturnsBinder()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithProviderAttribute_Provider))
            };

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection();

            providers.RegisterBinderForType(typeof(ModelWithProviderAttribute_Provider), new Mock <IExtensibleModelBinder>().Object, true /* suppressPrefix */);

            // Act
            IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.AreEqual(typeof(CustomBinder), binder.GetType(), "Binder should've come from attribute rather than collection.");
        }
예제 #7
0
        public void GetBinder_FromAttribute_Binder_ValueNotPresent_ReturnsNull()
        {
            // Arrange
            ControllerContext             controllerContext = new ControllerContext();
            ExtensibleModelBindingContext bindingContext    = new ExtensibleModelBindingContext()
            {
                ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ModelWithProviderAttribute_Binder)),
                ModelName     = "foo",
                ValueProvider = new SimpleValueProvider()
                {
                    { "bar", "barValue" }
                }
            };

            ModelBinderProviderCollection providers = new ModelBinderProviderCollection();

            providers.RegisterBinderForType(typeof(ModelWithProviderAttribute_Binder), new Mock <IExtensibleModelBinder>().Object, true /* suppressPrefix */);

            // Act
            IExtensibleModelBinder binder = providers.GetBinder(controllerContext, bindingContext);

            // Assert
            Assert.IsNull(binder, "Binder should've come from attribute rather than collection, even if the attribute's provider returns null.");
        }