public void Create_ForDictionaryType_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);

            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(KeyValuePair <string, int>) ||
                    m.ModelType == typeof(int) ||
                    m.ModelType == typeof(string))
                {
                    return(Mock.Of <IModelBinder>());
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return(null);
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            var binder = Assert.IsType <DictionaryModelBinder <string, int> >(result);

            Assert.False(binder.AllowValidatingTopLevelNodes); // work done in DictionaryModelBinder.
        }
Пример #2
0
        public void Create_ForDictionaryType_ReturnsBinder_WithExpectedAllowValidatingTopLevelNodes(
            bool allowValidatingTopLevelNodes)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(typeof(Dictionary <string, string>));

            context.MvcOptions.AllowValidatingTopLevelNodes = allowValidatingTopLevelNodes;
            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(KeyValuePair <string, string>) || m.ModelType == typeof(string))
                {
                    return(Mock.Of <IModelBinder>());
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return(null);
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            var binder = Assert.IsType <DictionaryModelBinder <string, string> >(result);

            Assert.Equal(allowValidatingTopLevelNodes, binder.AllowValidatingTopLevelNodes);
            Assert.False(((CollectionModelBinder <KeyValuePair <string, string> >)binder).AllowValidatingTopLevelNodes);
        }
        public void Create_ForDictionaryType_ReturnsBinder(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);
            context.OnCreatingBinder(m =>
            {
                if (m.ModelType == typeof(KeyValuePair<string, int>) ||
                    m.ModelType == typeof(int) ||
                    m.ModelType == typeof(string))
                {
                    return Mock.Of<IModelBinder>();
                }
                else
                {
                    Assert.False(true, "Not the right model type");
                    return null;
                }
            });

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.IsType<DictionaryModelBinder<string, int>>(result);
        }
        public void Create_ForNonDictionaryType_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }
        public void Create_ForNonDictionaryType_ReturnsNull(Type modelType)
        {
            // Arrange
            var provider = new DictionaryModelBinderProvider();

            var context = new TestModelBinderProviderContext(modelType);

            // Act
            var result = provider.GetBinder(context);

            // Assert
            Assert.Null(result);
        }