public void BindModel_ValidValueProviderResult_ReturnsTrue() { // Arrange ModelBindingContext bindingContext = GetBindingContext(); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", 42 } }; TypeMatchModelBinder binder = new TypeMatchModelBinder(); HttpActionContext actionContext = new HttpActionContext { ControllerContext = new HttpControllerContext { Configuration = new HttpConfiguration() } }; // Act bool retVal = binder.BindModel(actionContext, bindingContext); // Assert Assert.True(retVal); Assert.Equal(42, bindingContext.Model); Assert.True(bindingContext.ModelState.ContainsKey("theModelName")); }
public void BindModel_ThrowsArgument_IfConfigurationIsNullOnControllerContext() { // Arrange TypeMatchModelBinder binder = new TypeMatchModelBinder(); HttpActionContext actionContext = new HttpActionContext { ControllerContext = new HttpControllerContext() }; Customer model = new Customer { Age = 99999 }; ModelBindingContext bindingContext = GetBindingContext(typeof(Customer)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", model } }; // Act Assert.ThrowsArgument( () => binder.BindModel(actionContext, bindingContext), "actionContext", "HttpControllerContext.Configuration must not be null." ); }
public void BindModel_Performs_ValidationOnArrays() { // Arrange TypeMatchModelBinder binder = new TypeMatchModelBinder(); HttpActionContext actionContext = new HttpActionContext { ControllerContext = new HttpControllerContext { Configuration = new HttpConfiguration() } }; Customer[] model = new[] { new Customer { Age = 99999 } }; ModelBindingContext bindingContext = GetBindingContext(typeof(Customer[])); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", model } }; bindingContext.ModelState = actionContext.ModelState; // Act bool retVal = binder.BindModel(actionContext, bindingContext); // Assert Assert.True(retVal); Assert.Same(model, bindingContext.Model); Assert.True(actionContext.ModelState.ContainsKey("theModelName")); Assert.False(actionContext.ModelState.IsValid); Assert.Equal("The field Age must be between 0 and 100.", actionContext.ModelState["theModelName[0].Age"].Errors[0].ErrorMessage); }
public void BindModel_InvalidValueProviderResult_ReturnsFalse() { // Arrange ModelBindingContext bindingContext = GetBindingContext(); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "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_ValidValueProviderResult_ReturnsTrue() { // Arrange ModelBindingContext bindingContext = GetBindingContext(); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "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 void BindModel_ThrowsArgument_IfConfigurationIsNullOnControllerContext() { // Arrange TypeMatchModelBinder binder = new TypeMatchModelBinder(); HttpActionContext actionContext = new HttpActionContext { ControllerContext = new HttpControllerContext() }; Customer model = new Customer { Age = 99999 }; ModelBindingContext bindingContext = GetBindingContext(typeof(Customer)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", model } }; // Act Assert.ThrowsArgument(() => binder.BindModel(actionContext, bindingContext), "actionContext", "HttpControllerContext.Configuration must not be null."); }