public void GetFormattedErrorMessage_ReturnsFormattedString() { //Create an Entity var emptyContact = new Contact(); emptyContact.FirstName = null; emptyContact.LastName = null; //Create PropertyValidator var propertyValidator = new PropertyValidator<Contact, string>(contact => contact.LastName); //Create a rule RuleValidator<Contact, string> ruleValidator = new LengthBetween<Contact>(1, 5); //Create a context var context = new RuleValidatorContext<Contact, string>(emptyContact, propertyValidator, null); //create it like this? IOC? Factory? //IMessageStore messageStore = new ResourceMessageStore(); //string errorMessage = messageStore.GetFormattedDefaultMessage(ruleValidator.GetType().Name, context, ruleValidator.Parameters); var messageService = new MessageService(); var errorMessage = messageService.GetDefaultMessageAndFormat(new MessageContext(context, ruleValidator.GetType(), false, null, null), ruleValidator.Parameters); Assert.That(errorMessage, Is.Not.Null.Or.Empty); Assert.That(errorMessage, Is.StringContaining("Last Name")); Assert.That(errorMessage, Is.StringContaining("1")); Assert.That(errorMessage, Is.StringContaining("5")); //null: Search for Actual value but it's empty b/c the value is null }
public bool LengthBetween_IsValid(string propertyValue, int low, int high) { //Create Validator var validator = new LengthBetween <Contact>(low, high); RuleValidatorContext <Contact, string> context = BuildContextForLength(propertyValue); var notification = new ValidationNotification(); //Validate the validator only, return true of no error returned return(validator.Validate(context, null, notification)); }
public void Validate_OptionalProperty_WithNoValue_IsValid() { var emptyContact = new Contact(); emptyContact.FirstName = string.Empty; emptyContact.LastName = string.Empty; var propertyValidator = new PropertyValidator<Contact, string>(contact => contact.LastName); //add a single rule var lengthValidator = new LengthBetween<Contact>(1, 5); propertyValidator.AndRule(lengthValidator); //.Rules.Add(lengthValidator); var notification = new ValidationNotification(); //Validate var result = propertyValidator.Validate(emptyContact, null, notification); Assert.That(result, Is.True); Assert.That(notification.Errors, Is.Empty); }
public void Validate_OptionalProperty_WithNoValue_IsValid() { var emptyContact = new Contact(); emptyContact.FirstName = string.Empty; emptyContact.LastName = string.Empty; var propertyValidator = new PropertyValidator <Contact, string>(contact => contact.LastName); //add a single rule var lengthValidator = new LengthBetween <Contact>(1, 5); propertyValidator.AndRule(lengthValidator); //.Rules.Add(lengthValidator); var notification = new ValidationNotification(); //Validate var result = propertyValidator.Validate(emptyContact, null, notification); Assert.That(result, Is.True); Assert.That(notification.Errors, Is.Empty); }
public void GetFormattedErrorMessage_ReturnsFormattedString() { //Create an Entity var emptyContact = new Contact(); emptyContact.FirstName = null; emptyContact.LastName = null; //Create PropertyValidator var propertyValidator = new PropertyValidator <Contact, string>(contact => contact.LastName); //Create a rule RuleValidator <Contact, string> ruleValidator = new LengthBetween <Contact>(1, 5); //Create a context var context = new RuleValidatorContext <Contact, string>(emptyContact, propertyValidator, null); //create it like this? IOC? Factory? //IMessageStore messageStore = new ResourceMessageStore(); //string errorMessage = messageStore.GetFormattedDefaultMessage(ruleValidator.GetType().Name, context, ruleValidator.Parameters); var messageService = new MessageService(); var messageContext = new MessageContext(context, ruleValidator.GetType(), false, null, null); var paramValues = (from ruleParameter in ruleValidator.Params select(object) ruleParameter.GetParamValue()) .ToList(); var errorMessage = messageService.GetDefaultMessageAndFormat(messageContext, paramValues); Assert.That(errorMessage, Is.Not.Null.Or.Empty); Assert.That(errorMessage, Is.StringContaining("Last Name")); Assert.That(errorMessage, Is.StringContaining("1")); Assert.That(errorMessage, Is.StringContaining("5")); //null: Search for Actual value but it's empty b/c the value is null }
public void Validate_Property_With_PropertyNameOverrideExpression_IsValid() { var emptyContact = new Contact(); emptyContact.FirstName = "George's last name"; emptyContact.LastName = string.Empty; var propertyValidator = new PropertyValidator <Contact, string>(contact => contact.LastName); propertyValidator.PropertyNameOverrideExpression = new Func <Contact, string>(o => o.FirstName); //add a single rule var lengthValidator = new LengthBetween <Contact>(1, 5); propertyValidator.AndRule(lengthValidator); //Validate ValidationNotification notification = new ValidationNotification(); propertyValidator.Validate(emptyContact, null, notification); Assert.That(notification.Errors, Is.Not.Empty); }
public void Validate_Property_With_PropertyNameOverrideExpression_IsValid() { var emptyContact = new Contact(); emptyContact.FirstName = "George's last name"; emptyContact.LastName = string.Empty; var propertyValidator = new PropertyValidator<Contact, string>(contact => contact.LastName); propertyValidator.PropertyNameOverrideExpression = new Func<Contact, string>( o => o.FirstName); //add a single rule var lengthValidator = new LengthBetween<Contact>(1, 5); propertyValidator.AndRule(lengthValidator); //Validate ValidationNotification notification = new ValidationNotification(); propertyValidator.Validate(emptyContact, null, notification); Assert.That(notification.Errors, Is.Not.Empty); }
public bool LengthBetween_IsValid(string propertyValue, int low, int high) { //Create Validator var validator = new LengthBetween<Contact>(low, high); RuleValidatorContext<Contact, string> context = BuildContextForLength(propertyValue); var notification = new ValidationNotification(); //Validate the validator only, return true of no error returned return validator.Validate(context, null, notification); }