public void NullCheckFailedHandler_ModelStateValid_CallbackReturnsNull_DoesNothing() { // Arrange HttpActionContext context = ContextUtil.CreateActionContext(); ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs(context, null /* parentNode */); // Act ModelBinderErrorMessageProvider originalProvider = ModelBinderConfig.ValueRequiredErrorMessageProvider; try { ModelBinderConfig.ValueRequiredErrorMessageProvider = (ec, mm, value) => null; EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(modelMetadata, null /* incomingValue */); handler(validationNode, e); } finally { ModelBinderConfig.ValueRequiredErrorMessageProvider = originalProvider; } // Assert Assert.True(context.ModelState.IsValid); }
public void NullCheckFailedHandler_ModelStateAlreadyInvalid_DoesNothing() { // Arrange HttpActionContext context = ContextUtil.CreateActionContext(); context.ModelState.AddModelError("foo.bar", "Some existing error."); ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs( context, null /* parentNode */ ); // Act EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler( modelMetadata, null /* incomingValue */ ); handler(validationNode, e); // Assert Assert.False(context.ModelState.ContainsKey("foo")); }
public void NullCheckFailedHandler_ModelStateValid_AddsErrorString() { // Arrange HttpActionContext context = ContextUtil.CreateActionContext(); ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs(context, null /* parentNode */); // Act EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(modelMetadata, null /* incomingValue */); handler(validationNode, e); // Assert Assert.True(context.ModelState.ContainsKey("foo")); Assert.Equal("A value is required.", context.ModelState["foo"].Errors[0].ErrorMessage); }