Пример #1
0
        private static (DefaultModelBindingContext, IModelBinder) GetBinderAndContext(
            Type modelType,
            bool suppressBindingUndefinedValueToEnumType,
            object valueProviderValue)
        {
            var binderProviderContext = new TestModelBinderProviderContext(modelType);
            var modelName             = "theModelName";
            var bindingContext        = new DefaultModelBindingContext
            {
                ModelMetadata = binderProviderContext.Metadata,
                ModelName     = modelName,
                ModelState    = new ModelStateDictionary(),
                ValueProvider = new SimpleValueProvider()
                {
                    { modelName, valueProviderValue }
                }
            };
            var binderProvider = new EnumTypeModelBinderProvider(new MvcOptions
            {
                SuppressBindingUndefinedValueToEnumType = suppressBindingUndefinedValueToEnumType
            });
            var binder = binderProvider.GetBinder(binderProviderContext);

            return(bindingContext, binder);
        }
        public void ReturnsBinder_ForFlagsEnumType(Type modelType)
        {
            // Arrange
            var provider = new EnumTypeModelBinderProvider(allowBindingUndefinedValueToEnumType: true);
            var context  = new TestModelBinderProviderContext(modelType);

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

            // Assert
            Assert.IsType <EnumTypeModelBinder>(result);
        }
Пример #3
0
        public void DoesNotReturnBinder_ForNonEnumTypes(Type modelType)
        {
            // Arrange
            var provider = new EnumTypeModelBinderProvider(new MvcOptions());
            var context  = new TestModelBinderProviderContext(modelType);

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

            // Assert
            Assert.Null(result);
        }
Пример #4
0
        public void ReturnsBinder_ForFlagsEnumType(Type modelType)
        {
            // Arrange
            var provider = new EnumTypeModelBinderProvider(new MvcOptions());
            var context  = new TestModelBinderProviderContext(modelType);

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

            // Assert
            Assert.IsType <EnumTypeModelBinder>(result);
        }
Пример #5
0
        public async Task BindModel_AllowsBindingUndefinedValues_ToEnumTypes(
            Type modelType,
            string suppliedValue,
            long expected)
        {
            // Arrange
            var binderProviderContext = new TestModelBinderProviderContext(modelType);
            var binderInfo            = GetBinderAndContext(
                modelType,
                suppressBindingUndefinedValueToEnumType: false,
                valueProviderValue: new object[] { suppliedValue });
            var bindingContext = binderInfo.Item1;
            var binder         = binderInfo.Item2;

            // Act
            await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.True(bindingContext.Result.IsModelSet);
            Assert.IsType(GetUnderlyingType(modelType), bindingContext.Result.Model);
            Assert.Equal(expected, Convert.ToInt64(bindingContext.Result.Model));
        }