public void Build() { EntityValidatorBase <Customer> customerValidator = Substitute.For <EntityValidatorBase <Customer> >(); EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(customerValidator, Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name); entityValidationRuleBuilder.AddValidator(new EntityPropertyValidator(new NotNullValidator())); entityValidationRuleBuilder.AddValidator(new EntityPropertyValidator(new NotEmptyValidator())); Expression <Func <Customer, bool> > expression = x => x.Name != null; entityValidationRuleBuilder.SetSpecification(new Specification <Customer>(expression)); entityValidationRuleBuilder.Build(); Assert.AreEqual(2, customerValidator.EntityValidationRules.Count); EntityPropertyValidationRule <Customer, string> validationRule0 = (EntityPropertyValidationRule <Customer, string>)customerValidator.EntityValidationRules[0]; EntityPropertyValidationRule <Customer, string> validationRule1 = (EntityPropertyValidationRule <Customer, string>)customerValidator.EntityValidationRules[1]; Assert.AreEqual(typeof(NotNullValidator).Name, validationRule0.Validator.GetValidatorTypeName()); Assert.AreEqual(typeof(NotEmptyValidator).Name, validationRule1.Validator.GetValidatorTypeName()); Assert.AreEqual(expression, validationRule0.Specification.Predicate); Assert.AreEqual(expression, validationRule1.Specification.Predicate); Assert.AreSame(validationRule0.Specification, validationRule1.Specification); }
public void AddValidator() { EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(new CustomerValidator(), Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name); entityValidationRuleBuilder.AddValidator(new EntityPropertyValidator(new NotNullValidator())); entityValidationRuleBuilder.AddValidator(new EntityPropertyValidator(new NotEmptyValidator())); Assert.AreEqual(2, entityValidationRuleBuilder.Validators.Count); Assert.AreEqual(typeof(NotNullValidator).Name, entityValidationRuleBuilder.Validators[0].GetValidatorTypeName()); Assert.AreEqual(typeof(NotEmptyValidator).Name, entityValidationRuleBuilder.Validators[1].GetValidatorTypeName()); }
public void BuildShouldAddValidatorsToTheRuleSetWhenRuleSetIsGiven() { EntityValidatorBase <Customer> customerValidator = Substitute.For <EntityValidatorBase <Customer> >(); const string ruleSetName = "TestRuleSet"; EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(customerValidator, Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name, ruleSetName); entityValidationRuleBuilder.AddValidator(new EntityPropertyValidator(new NotNullValidator())); entityValidationRuleBuilder.AddValidator(new EntityPropertyValidator(new NotEmptyValidator())); Assert.AreEqual(ruleSetName, entityValidationRuleBuilder.RuleSetName); entityValidationRuleBuilder.Build(); Assert.AreEqual(1, customerValidator.AllEntityValidationRules.Count); IList <IEntityValidationRule <Customer> > entityValidationRules = customerValidator.AllEntityValidationRules[ruleSetName]; EntityPropertyValidationRule <Customer, string> validationRule0 = (EntityPropertyValidationRule <Customer, string>)entityValidationRules[0]; EntityPropertyValidationRule <Customer, string> validationRule1 = (EntityPropertyValidationRule <Customer, string>)entityValidationRules[1]; Assert.AreEqual(typeof(NotNullValidator).Name, validationRule0.Validator.GetValidatorTypeName()); Assert.AreEqual(typeof(NotEmptyValidator).Name, validationRule1.Validator.GetValidatorTypeName()); }
public void AddValidatorMustThrowArgumentNullExceptionWhenTheValidatorIsNull() { EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(new CustomerValidator(), Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name); entityValidationRuleBuilder.AddValidator(null); }