public async Task BindModelSetsModelToNullOnNullOrEmptyString(string value) { // Arrange var valueProvider = new SimpleValueProvider() { { "foo", value } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(binderResult.IsModelSet); Assert.Equal("foo", binderResult.Key); Assert.Null(binderResult.Model); var modelState = Assert.Single(bindingContext.ModelState); Assert.Equal("foo", modelState.Key); Assert.Equal(string.Empty, modelState.Value.RawValue); }
public async Task BindModel() { // Arrange var valueProvider = new SimpleValueProvider() { { "foo", "Fys1" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotNull(binderResult); var bytes = Assert.IsType <byte[]>(binderResult.Model); Assert.Equal(new byte[] { 23, 43, 53 }, bytes); }
public async Task BindModel_ReturnsWithIsModelSetFalse_WhenValueNotFound() { // Arrange var valueProvider = new SimpleValueProvider() { { "someName", "" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(binderResult.IsModelSet); Assert.Equal("foo", binderResult.Key); Assert.Null(binderResult.Model); Assert.Empty(bindingContext.ModelState); // No submitted data for "foo". }
public async Task BindModelAddsModelErrorsOnInvalidCharacters() { // Arrange var expected = "The value '\"Fys1\"' is not valid for Byte[]."; var valueProvider = new SimpleValueProvider() { { "foo", "\"Fys1\"" } }; var bindingContext = GetBindingContext(valueProvider, typeof(byte[])); var binder = new ByteArrayModelBinder(); // Act var binderResult = await binder.BindModelResultAsync(bindingContext); // Assert Assert.NotNull(binderResult); Assert.False(bindingContext.ModelState.IsValid); var error = Assert.Single(bindingContext.ModelState["foo"].Errors); Assert.Equal(expected, error.ErrorMessage); }