コード例 #1
0
        public async Task When_async_condition_executed_for_each_instance_of_RuleForEach_condition_should_not_be_cached()
        {
            var person = new Person {
                Children = new List <Person> {
                    new Person {
                        Id = 1
                    },
                    new Person {
                        Id = 0
                    }
                }
            };

            var childValidator = new InlineValidator <Person>();
            int executions     = 0;

            childValidator.WhenAsync(async(a, ct) => {
                executions++;
                return(a.Id != 0);
            }, () => {
                childValidator.RuleFor(a => a.Id).Equal(1);
            });
            var personValidator = new InlineValidator <Person>();

            personValidator.RuleForEach(p => p.Children).SetValidator(childValidator);

            var validationResult = await personValidator.ValidateAsync(person);

            validationResult.IsValid.ShouldBeTrue();
            executions.ShouldEqual(2);
        }
コード例 #2
0
        public async Task Shouldnt_break_with_hashcode_collision_async()
        {
            var v1 = new InlineValidator <Collision1>();
            var v2 = new InlineValidator <Collision2>();

            var v = new InlineValidator <CollisionBase>();

            v.WhenAsync((x, ct) => Task.FromResult(x is Collision1), () => {
                v.RuleFor(x => ((Collision1)x).Name).NotNull();
            });
            v.WhenAsync((x, ct) => Task.FromResult(x is Collision2), () => {
                v.RuleFor(x => ((Collision2)x).Name).NotNull();
            });

            var containerValidator = new InlineValidator <List <CollisionBase> >();

            containerValidator.RuleForEach(x => x).SetValidator(v);

            // shouldn't throw an InvalidCastException.
            await containerValidator.ValidateAsync(new List <CollisionBase> {
                new Collision1(), new Collision2()
            });
        }