public void CanTransferValidationResultsToModelState() { var validator = new Validator(); var invalidObject = new SomeObject { LastName = null, FirstName = "ThisFirstNameIsTooLong", Street = " ", MiddleName = "valid" }; Assert.IsFalse(validator.IsValid(invalidObject)); var results = validator.ValidationResultsFor(invalidObject); Assert.IsNotNull(results); Assert.AreEqual(3, results.Count, "Wrong number of validation messages encountered."); ModelStateDictionary modelState = new ModelStateDictionary(); Assert.IsNotNull(modelState); Assert.AreEqual(0, modelState.Values.Count); MvcValidationAdapter.TransferValidationMessagesTo(modelState, validator.ValidationResultsFor(invalidObject)); Assert.AreEqual(3, modelState.Values.Count); var resultsList = new List <string>(); foreach (var result in modelState.Values) { foreach (var errs in result.Errors) { resultsList.Add(errs.ErrorMessage); } } var errors = new[] { "Dude...the name please!!", "The Street field is required.", "The field FirstName must be a string with a maximum length of 10." }; Assert.AreEqual(resultsList.Count, errors.Length, "Number of error messages do not match"); foreach (var error in errors) { Assert.IsTrue(resultsList.Contains(error), "Expected error \"" + error + "\" not found"); } }
public void CanRetriveValiationResults() { var validator = new Validator(); var invalidObject = new SomeObject(); ICollection <IValidationResult> results = validator.ValidationResultsFor(invalidObject); Assert.AreEqual(1, results.Count); Assert.AreEqual("Name", results.First().PropertyName); Assert.AreEqual(typeof(SomeObject), results.First().ClassContext); Assert.AreEqual("Dude...the name please!!", results.First().Message); }
public void ValidateRequiredAttributeEmptyTest() { var validator = new Validator(); var objectTotest = CreateValidCompleteObject(); objectTotest.RequiredValidatorString = string.Empty; ICollection <IValidationResult> results = validator.ValidationResultsFor(objectTotest); Assert.IsNotNull(results); Assert.AreEqual(1, results.Count); Assert.AreEqual("RequiredValidatorString", results.First().PropertyName); Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext); Assert.AreEqual("The RequiredValidatorString field is required.", results.First().Message); }
public void ValidateRangeDoubleSpecialMessageIsNotValidTests() { var validator = new Validator(); var objectTotest = CreateValidCompleteObject(); objectTotest.RangeDoubleValidatorSpecialMessage = 2; ICollection <IValidationResult> results = validator.ValidationResultsFor(objectTotest); Assert.IsNotNull(results); Assert.AreEqual(1, results.Count); Assert.AreEqual("RangeDoubleValidatorSpecialMessage", results.First().PropertyName); Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext); Assert.AreEqual("Special Error message here!", results.First().Message); }
[TestMethod, Ignore] //Ignore vecause data annotations does not check list length public void ValidateRequiredAttributeEmptyList() { var validator = new Validator(); var objectTotest = CreateValidCompleteObject(); objectTotest.RequiredListString = new List <string>(); ICollection <IValidationResult> results = validator.ValidationResultsFor(objectTotest); Assert.IsNotNull(results); Assert.AreEqual(1, results.Count); Assert.AreEqual("RequiredListString", results.First().PropertyName); Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext); Assert.AreEqual("may not be null or empty", results.First().Message); }
public void CanValidateCompleteObject() { var validator = new Validator(); var invalidObject = CreateValidCompleteObject(); invalidObject.RequiredValidatorString = null; Assert.IsFalse(validator.IsValid(invalidObject)); var validObject = CreateValidCompleteObject(); var validationErrors = validator.ValidationResultsFor(validObject); Assert.IsTrue(validator.IsValid(validObject)); }
public void ValidateRangeDoubleLongIsNotValidTests() { var validator = new Validator(); var objectTotest = CreateValidCompleteObject(); var doubleList = new List <long> { -3, -1, 0, 1, 2, 11, 12 }; foreach (var list in doubleList) { objectTotest.RangeDoubleValidatorLong = list; ICollection <IValidationResult> results = validator.ValidationResultsFor(objectTotest); Assert.IsNotNull(results, "Failed on: \"" + list + "\""); Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\""); Assert.AreEqual("RangeDoubleValidatorLong", results.First().PropertyName, "Failed on: \"" + list + "\""); Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\""); Assert.AreEqual("The field RangeDoubleValidatorLong must be between 3 and 10.", results.First().Message, "Failed on: \"" + list + "\""); } }
public void ValidateRangeDoubleMaxOnlyIsNotValidTests() { var validator = new Validator(); var objectTotest = CreateValidCompleteObject(); var doubleList = new List <double> { 3.000001, 4, 2.0E200 }; foreach (var list in doubleList) { objectTotest.RangeDoubleValidatorMaxOnly = list; ICollection <IValidationResult> results = validator.ValidationResultsFor(objectTotest); Assert.IsNotNull(results, "Failed on: \"" + list + "\""); Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\""); Assert.AreEqual("RangeDoubleValidatorMaxOnly", results.First().PropertyName, "Failed on: \"" + list + "\""); Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\""); Assert.AreEqual("The field RangeDoubleValidatorMaxOnly must be between -1.79769313486232E+308 and 3.", results.First().Message, "Failed on: \"" + list + "\""); } }
public void ValidateRangeDoubleIsNotValidTests() { var validator = new Validator(); var objectTotest = CreateValidCompleteObject(); var doubleList = new List <double> { -1.1, -1, 0, 0.0001, 1, 1.0000001, 2.5000001, 3, 4, 4.1 }; foreach (var list in doubleList) { objectTotest.RangeDoubleValidator = list; ICollection <IValidationResult> results = validator.ValidationResultsFor(objectTotest); Assert.IsNotNull(results, "Failed on: \"" + list + "\""); Assert.AreEqual(1, results.Count, "Failed on: \"" + list + "\""); Assert.AreEqual("RangeDoubleValidator", results.First().PropertyName, "Failed on: \"" + list + "\""); Assert.AreEqual(typeof(CompleteObject), results.First().ClassContext, "Failed on: \"" + list + "\""); Assert.AreEqual("The field RangeDoubleValidator must be between 1.01 and 2.5.", results.First().Message, "Failed on: \"" + list + "\""); } }