public void GivenExceptionWithMultipeMembers_WhenAddModelErrors_ThenModelStateErrorAddedForEachMember() { string[] expected = new string[] { "Property1", "Property2", "AnotherProperty" }; ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(new ValidationResult(null, expected), null, null); target.AddModelErrors(exception); CollectionAssert.AreEqual(expected, target.Keys.ToList()); }
public void GivenExceptionCreatedWithoutValidationResult_WhenAddModelErrors_ThenModelStateErrorAddedWithMessage() { string expected = "even without a validation result this should be the message"; ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(expected); target.AddModelErrors(exception); Assert.AreEqual(expected, target.Values.Single().Errors.Single().ErrorMessage); }
public void GivenNullException_WhenAddModelErrors_ThenThrowException() { ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(); TestExtensions.ExpectException <ArgumentNullException>(() => target.AddModelErrors(null)); }
public void GivenExceptionNoMember_WhenAddModelErrors_ThenModelStateErrorAddedWithEmptyKey() { ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(new ValidationResult(null, null), null, null); target.AddModelErrors(exception); Assert.AreEqual(string.Empty, target.Keys.Single()); }
public static void Validate <T>(this ModelStateDictionary modelState, IValidator <T> validator, T instanceToValidate) { var result = validator.Validate(instanceToValidate); if (result.Errors.Any()) { modelState.AddModelErrors(new Error.ValidationFailed(result)); } }
public void GivenExceptionCreatedWithoutValidationResult_WhenAddModelErrors_ThenModelStateErrorAddedWithEmptyKey() { ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(null); target.AddModelErrors(exception); Assert.AreEqual(string.Empty, target.Keys.Single()); }
public void GivenExceptionNoMember_WhenAddModelErrors_ThenModelStateErrorAddedWithMessage() { string expected = "here is the error message"; ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(new ValidationResult(expected, null), null, null); target.AddModelErrors(exception); Assert.AreEqual(expected, target.Values.Single().Errors.Single().ErrorMessage); }
public void Dont_Add_Message_Twice_When_Taken_From_Errors_Collection() { var errors = new ErrorBuilder() .Add("a"); var modelState = new ModelStateDictionary(); modelState.AddModelErrors((Result)errors); Assert.AreEqual(1, modelState.Sum(m => m.Value.Errors.Count)); }
public void Add_Value_String() { var result = new Result(HttpStatusCode.BadRequest, "foo"); var modelState = new ModelStateDictionary(); modelState.AddModelErrors(result); Assert.AreEqual(1, modelState.Count); Assert.IsNotNull(modelState[""]); Assert.AreEqual(result.Value.ToString(), modelState[""].Errors[0].ErrorMessage); }
public void GivenExceptionWithMultipeMembers_WhenAddModelErrors_ThenErrorMessageAddedForEachMember() { string expected = "this error message should appear for each property"; string[] properties = new string[] { "Property1", "Property2", "AnotherProperty" }; ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(new ValidationResult(expected, properties), null, null); target.AddModelErrors(exception); string[] actual = target.Values.Select(v => v.Errors.Single().ErrorMessage).ToArray(); Assert.AreEqual(properties.Length, actual.Length); Assert.AreEqual(expected, actual.Distinct().Single()); }
public void Add_Even_If_Not_Error_Result() { var modelState = new ModelStateDictionary(); var result = new Result(HttpStatusCode.OK, "foo"); modelState.AddModelErrors(result); Assert.AreEqual(1, modelState.Count); modelState.Clear(); var errors = new ErrorBuilder() .Add("a", null, "a") .Add("b", null, "b") .GetErrors(); result = new Result(HttpStatusCode.OK, errors); modelState.AddModelErrors(result); Assert.AreEqual(errors.Count, modelState.Sum(m => m.Value.Errors.Count)); }
public void Add_Errors_From_Builder_To_ModelState() { var errors = new ErrorBuilder() .Add("a", null, "a") .Add("b", null, "b"); var result = new Result(HttpStatusCode.BadRequest, errors); var modelState = new ModelStateDictionary(); modelState.AddModelErrors(result); Assert.AreEqual(errors.Count, modelState.Sum(m => m.Value.Errors.Count)); }
public static void AddModelErrors(this ModelStateDictionary source, IEnumerable <ValidationResult> validationResults, bool unique = true) { if (source == null) { throw new ArgumentNullException("source"); } if (validationResults == null) { throw new ArgumentNullException("validationResult"); } foreach (var validationResult in validationResults) { source.AddModelErrors(validationResult, unique); } }
public void GivenNullException_WhenAddModelErrors_ThenThrowException() { ModelStateDictionary target = new ModelStateDictionary(); ValidationException exception = new ValidationException(); TestExtensions.ExpectException<ArgumentNullException>(() => target.AddModelErrors(null)); }
public void FillModelStateErrors(ModelStateDictionary modelState, TeamEntityViewModel teamEntityViewModel, EditModes insert) { modelState.AddModelErrors(_teamValidator.GetInsertValidationErrors()); modelState.AddModelErrors(_userValidator.GetInsertValidationErrors()); }
/// <summary> /// Convert a IEnumerable<string> of errors to a JsonError by adding it to the model state /// Uses the existing JsonAnswer.Error method to return the string in the correct json format /// as expected by ajax calls from the UI /// </summary> /// <param name="modelState"></param> /// <param name="errors"></param> /// <param name="resultsData"></param> /// <returns></returns> public static JsonResult ToJsonError(this ModelStateDictionary modelState, IEnumerable <string> errors, object resultsData = null) { modelState.AddModelErrors(errors); return(JsonAnswer.Error(modelState, resultsData)); }