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_ModelStateValid_CallbackReturnsNull_DoesNothing() { // Arrange ControllerContext controllerContext = new ControllerContext { Controller = new EmptyController() }; ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs(controllerContext, null /* parentNode */); // Act ModelBinderErrorMessageProvider originalProvider = ModelBinderConfig.ValueRequiredErrorMessageProvider; try { ModelBinderConfig.ValueRequiredErrorMessageProvider = delegate { return(null); }; EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(controllerContext, modelMetadata, null /* incomingValue */); handler(validationNode, e); } finally { ModelBinderConfig.ValueRequiredErrorMessageProvider = originalProvider; } // Assert Assert.True(controllerContext.Controller.ViewData.ModelState.IsValid); }
public void NullCheckFailedHandler_ModelStateValid_AddsErrorString() { // Arrange ControllerContext controllerContext = new ControllerContext { Controller = new EmptyController() }; ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs( controllerContext, null /* parentNode */ ); // Act EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler( controllerContext, modelMetadata, null /* incomingValue */ ); handler(validationNode, e); // Assert Assert.True(controllerContext.Controller.ViewData.ModelState.ContainsKey("foo")); Assert.Equal( "A value is required.", controllerContext.Controller.ViewData.ModelState["foo"].Errors[0].ErrorMessage ); }
public void NullCheckFailedHandler_ModelStateAlreadyInvalid_DoesNothing() { // Arrange ControllerContext controllerContext = new ControllerContext { Controller = new EmptyController() }; controllerContext.Controller.ViewData.ModelState.AddModelError( "foo.bar", "Some existing error." ); ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs( controllerContext, null /* parentNode */ ); // Act EventHandler <ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler( controllerContext, modelMetadata, null /* incomingValue */ ); handler(validationNode, e); // Assert Assert.False(controllerContext.Controller.ViewData.ModelState.ContainsKey("foo")); }
public void NullCheckFailedHandler_ModelStateValid_CallbackReturnsNull_DoesNothing() { // Arrange ControllerContext controllerContext = new ControllerContext { Controller = new EmptyController() }; ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs(controllerContext, null /* parentNode */); // Act ModelBinderErrorMessageProvider originalProvider = ModelBinderConfig.ValueRequiredErrorMessageProvider; try { ModelBinderConfig.ValueRequiredErrorMessageProvider = delegate { return null; }; EventHandler<ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(controllerContext, modelMetadata, null /* incomingValue */); handler(validationNode, e); } finally { ModelBinderConfig.ValueRequiredErrorMessageProvider = originalProvider; } // Assert Assert.True(controllerContext.Controller.ViewData.ModelState.IsValid); }
public void NullCheckFailedHandler_ModelStateValid_AddsErrorString() { // Arrange ControllerContext controllerContext = new ControllerContext { Controller = new EmptyController() }; ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs(controllerContext, null /* parentNode */); // Act EventHandler<ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(controllerContext, modelMetadata, null /* incomingValue */); handler(validationNode, e); // Assert Assert.True(controllerContext.Controller.ViewData.ModelState.ContainsKey("foo")); Assert.Equal("A value is required.", controllerContext.Controller.ViewData.ModelState["foo"].Errors[0].ErrorMessage); }
public void NullCheckFailedHandler_ModelStateAlreadyInvalid_DoesNothing() { // Arrange ControllerContext controllerContext = new ControllerContext { Controller = new EmptyController() }; controllerContext.Controller.ViewData.ModelState.AddModelError("foo.bar", "Some existing error."); ModelMetadata modelMetadata = GetMetadataForType(typeof(Person)); ModelValidationNode validationNode = new ModelValidationNode(modelMetadata, "foo"); ModelValidatedEventArgs e = new ModelValidatedEventArgs(controllerContext, null /* parentNode */); // Act EventHandler<ModelValidatedEventArgs> handler = MutableObjectModelBinder.CreateNullCheckFailedHandler(controllerContext, modelMetadata, null /* incomingValue */); handler(validationNode, e); // Assert Assert.False(controllerContext.Controller.ViewData.ModelState.ContainsKey("foo")); }