Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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());
        }