Ejemplo n.º 1
0
        public void Validate_Rules_ErrorNoError()
        {
            // if
            var errorRule = new Mock <IValidationRule <TestValidatableEntity> >();

            errorRule.Setup(r => r.Execute(It.IsAny <TestValidatableEntity>(), It.IsAny <NonBlockingValidationContext>()))
            .Returns(new ValidationError(It.IsAny <string>(), field: It.IsAny <string>()));

            var noErrorRule = new Mock <IValidationRule <TestValidatableEntity> >();

            noErrorRule.Setup(r => r.Execute(It.IsAny <TestValidatableEntity>(), It.IsAny <NonBlockingValidationContext>()))
            .Returns <ValidationError>(null);


            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(sp => sp.GetService(typeof(IEnumerable <IValidationRule <TestValidatableEntity> >)))
            .Returns(new List <IValidationRule <TestValidatableEntity> > {
                errorRule.Object, noErrorRule.Object
            });

            var validateable = new TestValidatableEntity();

            // when
            var validator = new NonBlockingValidator(serviceProvider.Object);

            validator.Validate(validateable);

            // then
            Assert.Single(validateable.ValidationErrors);
        }
Ejemplo n.º 2
0
        public void Validate_NoRules()
        {
            // if
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(sp => sp.GetService(typeof(IEnumerable <IValidationRule <TestValidatableEntity> >)))
            .Returns(new List <IValidationRule <TestValidatableEntity> >());

            var validateable = new TestValidatableEntity();

            // when
            var validator = new NonBlockingValidator(serviceProvider.Object);

            validator.Validate(validateable);

            // then
            Assert.Empty(validateable.ValidationErrors);
        }