Пример #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);
        }
        public void RuleForShouldCallEntityValidationRuleSetRulesetName()
        {
            EntityValidatorBase <Customer> entityValidator = Substitute.For <EntityValidatorBase <Customer> >();
            const string ruleSetName = "TestRuleSet";
            EntityValidationRuleSetBuilder <Customer> ruleSetBuilder = new EntityValidationRuleSetBuilder <Customer>(Substitute.For <IPropertyDisplayNameResolver>(), entityValidator, ruleSetName);

            EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = (EntityValidationRuleBuilder <Customer, string>)ruleSetBuilder.RuleFor(x => x.FirstName);

            Assert.AreEqual(ruleSetName, entityValidationRuleBuilder.RuleSetName);
        }
Пример #3
0
        public void SetSpecification()
        {
            EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(new CustomerValidator(), Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name);
            Expression <Func <Customer, bool> >            expression = x => x.Name != null;

            entityValidationRuleBuilder.SetSpecification(new Specification <Customer>(expression));

            Assert.IsNotNull(entityValidationRuleBuilder.Specification);
            Assert.AreEqual(expression, entityValidationRuleBuilder.Specification.Predicate);
        }
Пример #4
0
        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());
        }
Пример #5
0
        public void SetMessage()
        {
            EntityValidatorBase <Customer> customerValidator = Substitute.For <EntityValidatorBase <Customer> >();

            EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(customerValidator, Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name);
            const string message = "Costumer name cannot be empty.";

            entityValidationRuleBuilder.SetMessage(message);

            Assert.AreEqual(message, entityValidationRuleBuilder.Message);
        }
Пример #6
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());
        }
Пример #7
0
        public void SetSpecificationMustNotThrowArgumentNullExceptionWhenTheSpecificationIsNull()
        {
            EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(new CustomerValidator(), Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name);

            Assert.DoesNotThrow(() => entityValidationRuleBuilder.SetSpecification(null));
        }
Пример #8
0
        public void AddValidatorMustThrowArgumentNullExceptionWhenTheValidatorIsNull()
        {
            EntityValidationRuleBuilder <Customer, string> entityValidationRuleBuilder = new EntityValidationRuleBuilder <Customer, string>(new CustomerValidator(), Substitute.For <IPropertyDisplayNameResolver>(), x => x.Name);

            entityValidationRuleBuilder.AddValidator(null);
        }