public void BindSimpleModelPropagatesErrorsOnFailure() { // Arrange ValueProviderResult result = new ValueProviderResult("invalid", null, null); ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ModelType = typeof(int) }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object returnedValue = binder.BindSimpleModel(null, bindingContext, result); // Assert Assert.IsFalse(bindingContext.ModelState.IsValidField("foo"), "Foo should be an invalid field."); Assert.IsInstanceOfType(bindingContext.ModelState["foo"].Errors[0].Exception, typeof(InvalidOperationException)); Assert.AreEqual("The parameter conversion from type 'System.String' to type 'System.Int32' failed. See the inner exception for more information.", bindingContext.ModelState["foo"].Errors[0].Exception.Message); Assert.IsNull(returnedValue, "Should have returned null on failure."); }
public void BindSimpleModelChecksValueProviderResultRawValueType() { // Arrange ValueProviderResult result = new ValueProviderResult(new MemoryStream(), null, null); ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ModelType = typeof(Stream) }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object returnedValue = binder.BindSimpleModel(null, bindingContext, result); // Assert Assert.AreEqual(result, bindingContext.ModelState["foo"].Value, "ModelState should have been set."); Assert.AreSame(result.RawValue, returnedValue, "Should have returned the RawValue since it was of the correct type."); }
public void BindSimpleModelCanReturnCollectionTypes() { // Arrange ValueProviderResult result = new ValueProviderResult(new string[] { "42", "82" }, null, null); ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ModelType = typeof(IEnumerable<int>) }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object returnedValue = binder.BindSimpleModel(null, bindingContext, result); // Assert Assert.IsInstanceOfType(returnedValue, typeof(IEnumerable<int>), "Returned value was of incorrect type."); List<int> returnedValueAsList = ((IEnumerable<int>)returnedValue).ToList(); Assert.AreEqual(2, returnedValueAsList.Count); Assert.AreEqual(42, returnedValueAsList[0]); Assert.AreEqual(82, returnedValueAsList[1]); }
public void BindSimpleModelCanReturnStrings() { // Arrange ValueProviderResult result = new ValueProviderResult(new object[] { "42" }, null, null); ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ModelType = typeof(string) }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object returnedValue = binder.BindSimpleModel(null, bindingContext, result); // Assert Assert.AreEqual("42", returnedValue); }
public void BindSimpleModelCanReturnArrayTypes() { // Arrange ValueProviderResult result = new ValueProviderResult(42, null, null); ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "foo", ModelType = typeof(int[]) }; DefaultModelBinder binder = new DefaultModelBinder(); // Act object returnedValue = binder.BindSimpleModel(null, bindingContext, result); // Assert Assert.IsInstanceOfType(returnedValue, typeof(int[]), "Returned value was of incorrect type."); int[] returnedValueAsIntArray = (int[])returnedValue; Assert.AreEqual(1, returnedValueAsIntArray.Length); Assert.AreEqual(42, returnedValueAsIntArray[0]); }