public void SetPropertyDoesNothingIfPropertyIsReadOnly() { // Arrange MyModel model = new MyModel(); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = "theModel" }; PropertyDescriptor property = TypeDescriptor.GetProperties(model)["ReadOnlyProperty"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act helper.PublicSetProperty(null, bindingContext, property, 42); // Assert Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState should remain untouched."); }
public void SetPropertySuccess() { // Arrange MyModelWithBadPropertySetter model = new MyModelWithBadPropertySetter(); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = "theModel" }; PropertyDescriptor property = TypeDescriptor.GetProperties(model)["NormalInt"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act helper.PublicSetProperty(null, bindingContext, property, 42); // Assert Assert.AreEqual(42, model.NormalInt); Assert.AreEqual(0, bindingContext.ModelState.Count, "ModelState should remain untouched."); }
public void SetPropertyCapturesAnyExceptionThrown() { // Arrange MyModelWithBadPropertySetter model = new MyModelWithBadPropertySetter(); ModelBindingContext bindingContext = new ModelBindingContext() { Model = model, ModelName = "theModel" }; PropertyDescriptor property = TypeDescriptor.GetProperties(model)["BadInt"]; DefaultModelBinderHelper helper = new DefaultModelBinderHelper(); // Act helper.PublicSetProperty(null, bindingContext, property, 42); // Assert Assert.AreEqual(@"No earthly integer is valid for this method. Parameter name: value", bindingContext.ModelState["theModel.BadInt"].Errors[0].Exception.Message); }