public void OnPropertyValidatingReturnsFalseIfModelIsAlreadyInvalid() { // Arrange ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "theModel" }; bindingContext.ModelState.AddModelError("themodel.readwriteproperty.stuff", "An error."); PropertyDescriptor property = TypeDescriptor.GetProperties(typeof(MyModel))["ReadWriteProperty"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act bool returned = helper.PublicOnPropertyValidating(null, bindingContext, property, null); // Assert Assert.IsFalse(returned, "Value should not have passed validation."); Assert.IsFalse(bindingContext.ModelState.ContainsKey("theModel.ReadWriteProperty"), "Shouldn't have modified ModelState if already contained errors."); }
public void OnPropertyValidatingReturnsTrueOnSuccess() { // Arrange ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "theModel" }; PropertyDescriptor property = TypeDescriptor.GetProperties(typeof(MyModel))["ReadWriteProperty"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act bool returned = helper.PublicOnPropertyValidating(null, bindingContext, property, 42); // Assert Assert.IsTrue(returned, "Value should have passed validation."); Assert.AreEqual(0, bindingContext.ModelState.Count); }
public void OnPropertyValidatingReturnsFalseAndCreatesValueRequiredErrorIfNecessary() { // Arrange ModelBindingContext bindingContext = new ModelBindingContext() { ModelName = "theModel" }; PropertyDescriptor property = TypeDescriptor.GetProperties(typeof(MyModel))["ReadWriteProperty"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act bool returned = helper.PublicOnPropertyValidating(null, bindingContext, property, null); // Assert Assert.IsFalse(returned, "Value should not have passed validation."); Assert.AreEqual("A value is required.", bindingContext.ModelState["theModel.ReadWriteProperty"].Errors[0].ErrorMessage); }