Esempio n. 1
0
        public void Valdiator_succeeds_when_item_is_valid()
        {
            var instance = new TestRequest {
                Value = 42
            };
            var validator = new ValueForValidator <int>(TestInteger.Validator);

            var selector = ValidatorOptions.ValidatorSelectors.DefaultValidatorSelectorFactory();
            var context  = new ValidationContext(instance, new PropertyChain(), selector);
            var propertyValidatorContext = new PropertyValidatorContext(context, PropertyRule.Create <TestRequest, int>(t => t.Value), nameof(TestRequest.Value));

            var result = validator.Validate(propertyValidatorContext).ToList();

            Assert.Empty(result);
        }
Esempio n. 2
0
        public void Valdiator_sets_proper_validation_message_when_item_is_invalid()
        {
            var instance = new TestRequest {
                Value = -42
            };
            var validator = new ValueForValidator <int>(TestInteger.Validator);

            var selector = ValidatorOptions.ValidatorSelectors.DefaultValidatorSelectorFactory();
            var context  = new ValidationContext(instance, new PropertyChain(), selector);
            var propertyValidatorContext = new PropertyValidatorContext(context, PropertyRule.Create <TestRequest, int>(t => t.Value), nameof(TestRequest.Value));

            var result = validator.Validate(propertyValidatorContext).ToList();

            Assert.NotEmpty(result);
            Assert.Equal("Value must be above zero", result.First().ErrorMessage);
        }