public void DefaultModelBindersProvider_ProvidesInstancesOfModelBinders()
        {
            // Arrange
            var service = Mock.Of <ITestService>();
            var binder  = new TypeMatchModelBinder();
            var options = new MvcOptions();

            options.ModelBinders.Add(binder);
            options.ModelBinders.Add(typeof(TestModelBinder));
            var optionsAccessor = new Mock <IOptionsAccessor <MvcOptions> >();

            optionsAccessor.SetupGet(o => o.Options)
            .Returns(options);
            var activator       = new TypeActivator();
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
            .Returns(service);

            var provider = new DefaultModelBindersProvider(optionsAccessor.Object, activator, serviceProvider.Object);

            // Act
            var binders = provider.ModelBinders;

            // Assert
            Assert.Equal(2, binders.Count);
            Assert.Same(binder, binders[0]);
            var testModelBinder = Assert.IsType <TestModelBinder>(binders[1]);

            Assert.Same(service, testModelBinder.Service);
        }
Exemple #2
0
        public async Task BindModel_ValidValueProviderResult_ReturnsNotNull()
        {
            // Arrange
            var bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", 42 }
            };

            var binder     = new TypeMatchModelBinder();
            var modelState = bindingContext.ModelState;

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotNull(result);
            Assert.True(result.IsModelSet);
            Assert.Equal(42, result.Model);
            var key = Assert.Single(modelState.Keys);

            Assert.Equal("theModelName", key);
            Assert.Equal("42", modelState[key].Value.AttemptedValue);
            Assert.Equal(42, modelState[key].Value.RawValue);
        }
        public void GetCompatibleValueProviderResult_ValueProviderReturnsNull_ReturnsNull()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider();

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.IsNull(vpResult, "Should have been null because no key matched.");
        }
        public void GetCompatibleValueProviderResult_ValueProviderReturnsNull_ReturnsNull()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider();

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.Null(vpResult); // No key matched
        }
Exemple #5
0
        public async Task GetCompatibleValueProviderResult_ValueProviderReturnsNull_ReturnsNull()
        {
            // Arrange
            var bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider();

            // Act
            var vpResult = await TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.Null(vpResult); // No key matched
        }
        public void GetCompatibleValueProviderResult_ValueProviderResultValid_ReturnsValueProviderResult()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", 42 }
            };

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.NotNull(vpResult);
        }
        public void GetCompatibleValueProviderResult_ValueProviderResultRawValueIncorrect_ReturnsNull()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "not an integer" }
            };

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.Null(vpResult); // Raw value is the wrong type
        }
Exemple #8
0
        public async Task GetCompatibleValueProviderResult_ValueProviderResultRawValueIncorrect_ReturnsNull()
        {
            // Arrange
            var bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", "not an integer" }
            };

            // Act
            var vpResult = await TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.Null(vpResult); // Raw value is the wrong type
        }
Exemple #9
0
        public async Task GetCompatibleValueProviderResult_ValueProviderResultValid_ReturnsValueProviderResult()
        {
            // Arrange
            var bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", 42 }
            };

            // Act
            var vpResult = await TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.NotNull(vpResult);
        }
        public void BindModel_InvalidValueProviderResult_ReturnsFalse() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleValueProvider() {
                { "theModelName", "not an integer" }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.IsFalse(retVal);
            Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState shouldn't have been touched.");
        }
        public void GetCompatibleValueProviderResult_ValueProviderResultRawValueIncorrect_ReturnsNull()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider()
            {
                { "theModelName", "not an integer" }
            };

            // Act
            ValueProviderResult vpResult = TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext);

            // Assert
            Assert.IsNull(vpResult, "Should have been null because the RawValue is of the wrong type.");
        }
        public void BindModel_ValidValueProviderResult_ReturnsTrue() {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleValueProvider() {
                { "theModelName", 42 }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.IsTrue(retVal);
            Assert.AreEqual(42, bindingContext.Model);
            Assert.IsTrue(bindingContext.ModelState.ContainsKey("theModelName"), "ModelState should've been updated.");
        }
        public void BindModel_InvalidValueProviderResult_ReturnsFalse()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "not an integer" }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.False(retVal);
            Assert.Empty(bindingContext.ModelState);
        }
        public void BindModel_InvalidValueProviderResult_ReturnsFalse()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider
            {
                { "theModelName", "not an integer" }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.False(retVal);
            Assert.Empty(bindingContext.ModelState);
        }
        public void BindModel_InvalidValueProviderResult_ReturnsFalse()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider()
            {
                { "theModelName", "not an integer" }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.IsFalse(retVal);
            Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState shouldn't have been touched.");
        }
Exemple #16
0
        public void BindModel_ValidValueProviderResult_ReturnsTrue()
        {
            // Arrange
            ExtensibleModelBindingContext bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleValueProvider {
                { "theModelName", 42 }
            };

            TypeMatchModelBinder binder = new TypeMatchModelBinder();

            // Act
            bool retVal = binder.BindModel(null, bindingContext);

            // Assert
            Assert.True(retVal);
            Assert.Equal(42, bindingContext.Model);
            Assert.True(bindingContext.ModelState.ContainsKey("theModelName"));
        }
        public async Task BindModel_InvalidValueProviderResult_ReturnsNull()
        {
            // Arrange
            var bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", "not an integer" }
            };

            var binder = new TypeMatchModelBinder();
            var modelState = bindingContext.ModelState;

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Null(result);
            Assert.Empty(modelState.Keys);
            Assert.True(modelState.IsValid);
        }
Exemple #18
0
        public async Task BindModel_InvalidValueProviderResult_ReturnsNull()
        {
            // Arrange
            var bindingContext = GetBindingContext();

            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", "not an integer" }
            };

            var binder     = new TypeMatchModelBinder();
            var modelState = bindingContext.ModelState;

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.Null(result);
            Assert.Empty(modelState.Keys);
            Assert.True(modelState.IsValid);
        }
        public async Task BindModel_ValidValueProviderResult_ReturnsNotNull()
        {
            // Arrange
            var bindingContext = GetBindingContext();
            bindingContext.ValueProvider = new SimpleHttpValueProvider
            {
                { "theModelName", 42 }
            };

            var binder = new TypeMatchModelBinder();
            var modelState = bindingContext.ModelState;

            // Act
            var result = await binder.BindModelAsync(bindingContext);

            // Assert
            Assert.NotNull(result);
            Assert.True(result.IsModelSet);
            Assert.Equal(42, result.Model);
            var key = Assert.Single(modelState.Keys);
            Assert.Equal("theModelName", key);
            Assert.Equal("42", modelState[key].Value.AttemptedValue);
            Assert.Equal(42, modelState[key].Value.RawValue);
        }
 public override IModelBinder GetBinder(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
 {
     return((TypeMatchModelBinder.GetCompatibleValueProviderResult(bindingContext) != null)
         ? new TypeMatchModelBinder()
         : null /* no match */);
 }