Exemple #1
0
        public void GetBinderThrowsIfBindingContextIsNull()
        {
            // Arrange
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IEnumerable <>), typeof(CollectionModelBinder <>));

            // Act & assert
            Assert.ThrowsArgumentNull(
                () => provider.GetBinder(null, null),
                "bindingContext");
        }
Exemple #2
0
        public void GetBinder_TypeDoesNotMatch_ModelTypeIsNotInterface_ReturnsNull()
        {
            // Arrange
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), typeof(CollectionModelBinder <>))
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(object));

            // Act
            IModelBinder binder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(binder);
        }
Exemple #3
0
        public void GetBinder_TypeMatches_Success_TypeActivation_ReturnsBinder()
        {
            // Arrange
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), typeof(CollectionModelBinder <>))
            {
                SuppressPrefixCheck = true
            };

            ModelBindingContext bindingContext = GetBindingContext(typeof(List <int>));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.IsType <CollectionModelBinder <int> >(returnedBinder);
        }
Exemple #4
0
        public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull()
        {
            // Arrange
            IModelBinder binderInstance         = new Mock <IModelBinder>().Object;
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), binderInstance);

            ModelBindingContext bindingContext = GetBindingContext(typeof(List <int>));

            bindingContext.ValueProvider = new SimpleHttpValueProvider();

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(returnedBinder);
        }
Exemple #5
0
        public void GetBinder_TypeMatches_Success_Instance_ReturnsBinder()
        {
            // Arrange
            IModelBinder binderInstance = new Mock <IModelBinder>().Object;

            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List <>), binderInstance)
            {
                SuppressPrefixCheck = true
            };

            ModelBindingContext bindingContext = GetBindingContext(typeof(List <int>));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Same(binderInstance, returnedBinder);
        }
Exemple #6
0
        public void GetBinder_TypeMatches_Success_Factory_ReturnsBinder()
        {
            // Arrange
            IModelBinder binderInstance = new Mock <IModelBinder>().Object;

            Func <Type[], IModelBinder> binderFactory = typeArguments =>
            {
                Assert.Equal(new[] { typeof(int) }, typeArguments);
                return(binderInstance);
            };

            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IList <>), binderFactory)
            {
                SuppressPrefixCheck = true
            };

            ModelBindingContext bindingContext = GetBindingContext(typeof(List <int>));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Same(binderInstance, returnedBinder);
        }
        public void GetBinderThrowsIfBindingContextIsNull()
        {
            // Arrange
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IEnumerable<>), typeof(CollectionModelBinder<>));

            // Act & assert
            Assert.ThrowsArgumentNull(
                () => provider.GetBinder(null, null),
                "bindingContext");
        }
        public void GetBinder_TypeMatches_Success_TypeActivation_ReturnsBinder()
        {
            // Arrange
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), typeof(CollectionModelBinder<>))
            {
                SuppressPrefixCheck = true
            };

            ModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.IsType<CollectionModelBinder<int>>(returnedBinder);
        }
        public void GetBinder_TypeMatches_Success_Instance_ReturnsBinder()
        {
            // Arrange
            IModelBinder binderInstance = new Mock<IModelBinder>().Object;

            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), binderInstance)
            {
                SuppressPrefixCheck = true
            };

            ModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Same(binderInstance, returnedBinder);
        }
        public void GetBinder_TypeMatches_Success_Factory_ReturnsBinder()
        {
            // Arrange
            IModelBinder binderInstance = new Mock<IModelBinder>().Object;

            Func<Type[], IModelBinder> binderFactory = typeArguments =>
            {
                Assert.Equal(new[] { typeof(int) }, typeArguments);
                return binderInstance;
            };

            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(IList<>), binderFactory)
            {
                SuppressPrefixCheck = true
            };

            ModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Same(binderInstance, returnedBinder);
        }
        public void GetBinder_TypeMatches_PrefixNotFound_ReturnsNull()
        {
            // Arrange
            IModelBinder binderInstance = new Mock<IModelBinder>().Object;
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), binderInstance);

            ModelBindingContext bindingContext = GetBindingContext(typeof(List<int>));
            bindingContext.ValueProvider = new SimpleHttpValueProvider();

            // Act
            IModelBinder returnedBinder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(returnedBinder);
        }
        public void GetBinder_TypeDoesNotMatch_ModelTypeIsNotInterface_ReturnsNull()
        {
            // Arrange
            GenericModelBinderProvider provider = new GenericModelBinderProvider(typeof(List<>), typeof(CollectionModelBinder<>))
            {
                SuppressPrefixCheck = true
            };
            ModelBindingContext bindingContext = GetBindingContext(typeof(object));

            // Act
            IModelBinder binder = provider.GetBinder(null, bindingContext);

            // Assert
            Assert.Null(binder);
        }