Esempio n. 1
0
        public void For_WithMemberAndName_ReturnsMemberValidationRule()
        {
            var validatorStub = new FooValidator();

            var rule = validatorStub.For(p => p.Bar, nameof(Foo.Bar));

            Assert.IsType <MemberValidationRule <Foo, string> >(rule);
        }
Esempio n. 2
0
        public async Task ValidateAsync_AllRulesSucceed_DoesNotThrow()
        {
            var foo = new Foo();

            var ruleMocks = new List <Mock <IValidationRule <Foo> > >
            {
                this.SetupRuleMock(foo, ValidationResult.Valid)
            };

            var validatorStub = new FooValidator(ruleMocks.Select(p => p.Object).ToList());

            await validatorStub.ValidateAsync(foo);
        }
Esempio n. 3
0
        public async Task ValidateAsync_OneRuleFails_Throws()
        {
            var foo = new Foo();

            var ruleMocks = new List <Mock <IValidationRule <Foo> > >
            {
                this.SetupRuleMock(foo, ValidationResult.Error("rule1"))
            };

            var validatorStub = new FooValidator(ruleMocks.Select(p => p.Object).ToList());

            var exception = await Assert.ThrowsAsync <ValidationException>(() => validatorStub.ValidateAsync(foo));

            Assert.NotNull(exception.Result);
            Assert.False(exception.Result.IsValid);
            Assert.Equal("rule1", exception.Result.Errors.Single().Message);
        }
Esempio n. 4
0
        public async Task TryValidateAsync_OneRuleFails_ReturnsInvalidResult()
        {
            var foo = new Foo();

            var ruleMocks = new List <Mock <IValidationRule <Foo> > >
            {
                this.SetupRuleMock(foo, ValidationResult.Error("rule1"))
            };

            var validatorStub = new FooValidator(ruleMocks.Select(p => p.Object).ToList());

            var result = await validatorStub.TryValidateAsync(foo);

            ruleMocks.ForEach(p => p.Verify(mock => mock.EvaluateAsync(foo), Times.Once()));
            Assert.False(result.IsValid);
            Assert.Equal("rule1", result.Errors.Single().Message);
        }
Esempio n. 5
0
        public async Task TryValidateAsync_AllRulesSucceed_ReturnsValidResult()
        {
            var foo = new Foo();

            var ruleMocks = new List <Mock <IValidationRule <Foo> > >
            {
                this.SetupRuleMock(foo, ValidationResult.Valid),
                this.SetupRuleMock(foo, ValidationResult.Valid)
            };

            var validatorStub = new FooValidator(ruleMocks.Select(p => p.Object).ToList());

            var result = await validatorStub.TryValidateAsync(foo);

            ruleMocks.ForEach(p => p.Verify(mock => mock.EvaluateAsync(foo), Times.Once()));
            Assert.True(result.IsValid);
        }
Esempio n. 6
0
        public void AddRule_WithoutRule_Throws()
        {
            var validatorStub = new FooValidator();

            Assert.Throws <ArgumentNullException>(() => validatorStub.AddRule(null));
        }
Esempio n. 7
0
        public void For_WithoutName_Throws()
        {
            var validatorStub = new FooValidator();

            Assert.Throws <ArgumentNullException>(() => validatorStub.For <string>(p => p.Bar, null));
        }
Esempio n. 8
0
        public void For_WithoutValueExpression_Throws()
        {
            var validatorStub = new FooValidator();

            Assert.Throws <ArgumentNullException>(() => validatorStub.For <string>(null, nameof(Foo.Bar)));
        }