public void ValidateAllShouldUseAllValidationElements()
        {
            var el1ValidationResults = new object[]
            {
                new ValidationResult(PropertyToValidate, new[] { PropertyToValidate }),
                new ValidationResult(PropertyToValidate, new[] { PropertyToValidate }),
            };
            var el2ValidationResults = new object[]
            {
                new ValidationResult(PropertyToMap, new[] { PropertyToMap }),
                new ValidationResult(PropertyToMap, new[] { PropertyToMap }),
            };
            bool el1IsInvoked = false;
            bool el2IsInvoked = false;
            var  item         = new object();
            var  el1          = new ValidationElementMock();
            var  el2          = new ValidationElementMock();

            el1.Validate = context =>
            {
                context.ObjectInstance.ShouldEqual(item);
                context.ServiceProvider.ShouldEqual(IocContainer);
                el1IsInvoked = true;
                return(el1ValidationResults);
            };
            el2.Validate = context =>
            {
                el2IsInvoked = true;
                return(el2ValidationResults);
            };
            DataAnnotationValidatior.GetValidationElementsDelegate =
                o => new Dictionary <string, List <DataAnnotationValidatior.IValidationElement> >
            {
                { PropertyToValidate, new List <DataAnnotationValidatior.IValidationElement> {
                      el1
                  } },
                { PropertyToMap, new List <DataAnnotationValidatior.IValidationElement> {
                      el2
                  } }
            };
            var validator = (DataAnnotationValidatior)GetValidator();

            validator.Initialize(new ValidatorContext(item, GetServiceProvider())).ShouldBeTrue();

            validator.ValidateAsync().Wait();
            el1IsInvoked.ShouldBeTrue();
            el2IsInvoked.ShouldBeTrue();

            var allErrors = validator.GetErrors();

            allErrors.Count.ShouldEqual(2);
            allErrors[PropertyToValidate].ShouldEqual(el1ValidationResults);
            allErrors[PropertyToMap].ShouldEqual(el2ValidationResults);
        }
Esempio n. 2
0
        public void ValidateShouldUseOnlyValidationElementsWithPropertyName()
        {
            var el1ValidationResults = new IValidationResult[]
            {
                new ValidationResult(PropertyToValidate, new[] { PropertyToValidate }),
                new ValidationResult(PropertyToValidate, new[] { PropertyToValidate }),
            };
            var el2ValidationResults = new IValidationResult[]
            {
                new ValidationResult(PropertyToMap, new[] { PropertyToMap }),
                new ValidationResult(PropertyToMap, new[] { PropertyToMap }),
            };
            bool el1IsInvoked = false;
            bool el2IsInvoked = false;
            var  item         = new object();
            var  el1          = new ValidationElementMock();
            var  el2          = new ValidationElementMock();

            el1.Validate = context =>
            {
                context.ObjectInstance.ShouldEqual(item);
                context.ServiceProvider.ShouldEqual(IocContainer);
                el1IsInvoked = true;
                return(el1ValidationResults);
            };
            el2.Validate = context =>
            {
                el2IsInvoked = true;
                return(el2ValidationResults);
            };
            ValidationElementProvider.GetValidationElements =
                o => new Dictionary <string, IList <IValidationElement> >
            {
                { PropertyToValidate, new IValidationElement[] { el1 } },
                { PropertyToMap, new IValidationElement[] { el2 } }
            };
            var validator = (ValidationElementValidator)GetValidator();

            validator.Initialize(new ValidatorContext(item, GetServiceProvider()));

            validator.ValidateAsync(PropertyToValidate).Wait();
            el1IsInvoked.ShouldBeTrue();
            el2IsInvoked.ShouldBeFalse();

            var allErrors = validator.GetErrors();

            allErrors.Count.ShouldEqual(1);
            allErrors[PropertyToValidate].ShouldEqual(el1ValidationResults);
        }