public void BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState_ErrorNotAddedIfCallbackReturnsNull() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleValueProvider { { "theModelName", "not an integer" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act ModelBinderErrorMessageProvider originalProvider = ModelBinderConfig.TypeConversionErrorMessageProvider; bool retVal; try { ModelBinderConfig.TypeConversionErrorMessageProvider = delegate { return null; }; retVal = binder.BindModel(null, bindingContext); } finally { ModelBinderConfig.TypeConversionErrorMessageProvider = originalProvider; } // Assert Assert.False(retVal); Assert.Null(bindingContext.Model); Assert.True(bindingContext.ModelState.IsValid); }
public void BindModel_NullValueProviderResult_ReturnsFalse() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.IsFalse(retVal, "BindModel should have returned null."); Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState shouldn't have been touched."); }
public void BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleValueProvider() { { "theModelName", "not an integer" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.IsFalse(retVal); Assert.AreEqual("The value 'not an integer' is not valid for Int32.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage); }
public async Task BindModel_ReturnsNull_IfTypeCannotBeConverted(Type destinationType) { // Arrange var bindingContext = GetBindingContext(destinationType); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "some-value" } }; var binder = new TypeConverterModelBinder(); // Act var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.Null(retVal); }
public void BindModel_Error_GeneralExceptionsSavedInModelState() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(Dummy)); bindingContext.ValueProvider = new SimpleValueProvider() { { "theModelName", "foo" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.IsFalse(retVal); Assert.IsNull(bindingContext.Model); Assert.AreEqual("The parameter conversion from type 'System.String' to type 'Microsoft.Web.Mvc.ModelBinding.Test.TypeConverterModelBinderTest+Dummy' failed. See the inner exception for more information.", bindingContext.ModelState["theModelName"].Errors[0].Exception.Message); Assert.AreEqual("From DummyTypeConverter: foo", bindingContext.ModelState["theModelName"].Errors[0].Exception.InnerException.Message); }
public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange ModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "not an integer" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.True(retVal); Assert.Null(bindingContext.Model); Assert.Equal(false, bindingContext.ModelState.IsValid); Assert.Equal("Input string was not in a correct format.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage); }
public async Task BindModel_CreatesError_WhenTypeConversionIsNull(Type destinationType) { // Arrange var bindingContext = GetBindingContext(destinationType); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", string.Empty } }; var binder = new TypeConverterModelBinder(); // Act var result = await binder.BindModelAsync(bindingContext); // Assert Assert.False(result.IsModelSet); Assert.NotNull(result.ValidationNode); var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors); Assert.Equal(error.ErrorMessage, "The value '' is invalid.", StringComparer.Ordinal); Assert.Null(error.Exception); }
public void BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(string)); bindingContext.ValueProvider = new SimpleValueProvider { { "theModelName", "" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.True(retVal); Assert.Null(bindingContext.Model); Assert.True(bindingContext.ModelState.ContainsKey("theModelName")); }
public void BindModel_ValidValueProviderResult_ReturnsModel() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleValueProvider() { { "theModelName", "42" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // 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 async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange ModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "not an integer" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.True(retVal); Assert.Null(bindingContext.Model); Assert.Equal(false, bindingContext.ModelState.IsValid); Assert.Equal("Input string was not in a correct format.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage); }
public async Task BindModelAsync_InvalidDate_ErrorMessage() { using (new CultureInfoScope("nl-NL")) { var context = GetBindingContext(typeof(Date)); context.ValueProvider = new SimpleValueProvider { { TheModelName, "Rubbish" } }; var binder = new TypeConverterModelBinder(); await binder.BindModelAsync(context); Assert.AreEqual(ModelValidationState.Invalid, context.ModelState.ValidationState); var error = context.ModelState[TheModelName].Errors.FirstOrDefault(); Assert.AreEqual("Geen geldige datum", error.ErrorMessage); Assert.IsNull(error.Exception); } }
public void BindModel_Error_GeneralExceptionsSavedInModelState() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(Dummy)); bindingContext.ValueProvider = new SimpleValueProvider { { "theModelName", "foo" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.False(retVal); Assert.Null(bindingContext.Model); Assert.Equal("The parameter conversion from type 'System.String' to type 'Microsoft.Web.Mvc.ModelBinding.Test.TypeConverterModelBinderTest+Dummy' failed. See the inner exception for more information.", bindingContext.ModelState["theModelName"].Errors[0].Exception.Message); Assert.Equal("From DummyTypeConverter: foo", bindingContext.ModelState["theModelName"].Errors[0].Exception.InnerException.Message); }
public async Task BindModel_CreatesError_WhenTypeConversionIsNull(Type destinationType) { // Arrange var bindingContext = GetBindingContext(destinationType); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", string.Empty } }; var binder = new TypeConverterModelBinder(); // Act var result = await binder.BindModelAsync(bindingContext); // Assert Assert.False(result.IsModelSet); Assert.NotNull(result.ValidationNode); var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors); Assert.Equal(error.ErrorMessage, "The value '' is invalid.", StringComparer.Ordinal); Assert.Null(error.Exception); }
public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange var message = "The value 'not an integer' is not valid for theModelName."; var bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "not an integer" } }; var binder = new TypeConverterModelBinder(); // Act var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(retVal); Assert.Null(retVal.Model); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors); Assert.Equal(message, error.ErrorMessage); }
public void BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleValueProvider { { "theModelName", "not an integer" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.False(retVal); Assert.Equal( "The value 'not an integer' is not valid for Int32.", bindingContext.ModelState["theModelName"].Errors[0].ErrorMessage ); }
public async Task BindModel_Error_FormatExceptionsTurnedIntoStringsInModelState() { // Arrange var message = "The value 'not an integer' is not valid for theModelName."; var bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "not an integer" } }; var binder = new TypeConverterModelBinder(); // Act var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(retVal); Assert.Null(retVal.Model); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["theModelName"].Errors); Assert.Equal(message, error.ErrorMessage); }
public async Task BindModel_ReturnsNotNull_IfTypeCanBeConverted(Type destinationType) { if (TestPlatformHelper.IsMono && destinationType == typeof(DateTimeOffset)) { // DateTimeOffset doesn't have a TypeConverter in Mono return; } // Arrange var bindingContext = GetBindingContext(destinationType); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "some-value" } }; var binder = new TypeConverterModelBinder(); // Act var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(retVal); }
public void BindModel_NullValueProviderResult_ReturnsFalse() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.False(retVal, "BindModel should have returned null."); Assert.Empty(bindingContext.ModelState); }
public void BindModel_ValidValueProviderResult_ReturnsModel() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleValueProvider { { "theModelName", "42" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // 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_ValidValueProviderResult_ConvertEmptyStringsToNull() { // Arrange ExtensibleModelBindingContext bindingContext = GetBindingContext(typeof(string)); bindingContext.ValueProvider = new SimpleValueProvider { { "theModelName", "" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = binder.BindModel(null, bindingContext); // Assert Assert.True(retVal); Assert.Null(bindingContext.Model); Assert.True(bindingContext.ModelState.ContainsKey("theModelName")); }
public async Task BindModel_NullValueProviderResult_ReturnsNull() { // Arrange var bindingContext = GetBindingContext(typeof(int)); var binder = new TypeConverterModelBinder(); // Act var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.Null(retVal); Assert.Empty(bindingContext.ModelState); }
public async Task BindModel_ValidValueProviderResult_ConvertEmptyStringsToNull() { // Arrange ModelBindingContext bindingContext = GetBindingContext(typeof(string)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "" } }; TypeConverterModelBinder binder = new TypeConverterModelBinder(); // Act bool retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.True(retVal); Assert.Null(bindingContext.Model); Assert.True(bindingContext.ModelState.ContainsKey("theModelName")); }
public async Task BindModel_ValidValueProviderResult_ReturnsModel() { // Arrange var bindingContext = GetBindingContext(typeof(int)); bindingContext.ValueProvider = new SimpleHttpValueProvider { { "theModelName", "42" } }; var binder = new TypeConverterModelBinder(); // Act var retVal = await binder.BindModelAsync(bindingContext); // Assert Assert.NotNull(retVal); Assert.Equal(42, retVal.Model);; Assert.True(bindingContext.ModelState.ContainsKey("theModelName")); }