Exemple #1
0
        public void For_SetupAsync_When_AsyncPropertyValidatorForExpressionAlreadyExists_Then_ItIsReturned()
        {
            // Arrange:
            var rootAsyncValidator = new RootAsyncValidatorImplementation();
            Expression <Func <RootValidatorsTestsCommon.TestClass, string> > expression = obj => obj.SomeProperty;
            var propertyValidator = rootAsyncValidator.SetupAsync(expression);

            // Act:
            var result = rootAsyncValidator.SetupAsync(expression);

            // Assert:
            result.Should().Be(propertyValidator);
        }
Exemple #2
0
        public async Task For_Validate_When_RootValidatorHasSetUpAsyncPropertyValidatorAndValidObjectIsPassed_Then_ValidationErrorsAreReturned()
        {
            // Arrange:
            var rootAsyncValidator = new RootAsyncValidatorImplementation();
            Expression <Func <RootValidatorsTestsCommon.TestClass, string> > expression = obj => obj.SomeProperty;

            rootAsyncValidator.SetupAsync(x => x.SomeProperty)
            .AddRule(async x =>
            {
                await Task.CompletedTask;
                return(x != null);
            }, "cannot be null", "NOT_NULL");
            var toValidate = (object)new RootValidatorsTestsCommon.TestClass
            {
                SomeProperty = null
            };

            // Act:
            var validationErrors = await rootAsyncValidator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("cannot be null");
            validationErrors[0].ErrorCode.Should().Be("NOT_NULL");
            validationErrors[0].PropertyPath.IsEmpty.Should().Be(false);
            validationErrors[0].ProvidedValue.Should().Be(null);
        }
Exemple #3
0
        public void For_SetupAsync_When_ProvidedExpressionIsNull_Then_ExceptionIsThrown()
        {
            // Arrange:
            var rootAsyncValidator = new RootAsyncValidatorImplementation();
            Expression <Func <RootValidatorsTestsCommon.TestClass, string> > expression = null;

            // Act & Assert:
            Assert.Throws <ArgumentNullException>(() => rootAsyncValidator.SetupAsync(expression));
        }
Exemple #4
0
        public void For_SetupAsync_When_SyncPropertyValidatorForExpressionAlreadyExists_Then_ExceptionIsThrown()
        {
            // Arrange:
            var rootAsyncValidator = new RootAsyncValidatorImplementation();
            Expression <Func <RootValidatorsTestsCommon.TestClass, string> > expression = obj => obj.SomeProperty;
            var propertyValidator = rootAsyncValidator.Setup(expression);

            // Act & Assert:
            var exception = Assert.Throws <InvalidOperationException>(() => rootAsyncValidator.SetupAsync(expression));

            exception.Message.Should().Be("Member already has assigned synchronous validator");
        }
Exemple #5
0
        public void For_SetupAsync_When_ThereIsNoPropertyValidatorForExpression_Then_NewPropertyValidatorIsCreated()
        {
            // Arrange:
            var rootAsyncValidator = new RootAsyncValidatorImplementation();
            Expression <Func <RootValidatorsTestsCommon.TestClass, string> > expression = obj => obj.SomeProperty;

            // Act:
            var result = rootAsyncValidator.SetupAsync(expression);

            // Assert:
            result.Should().BeAssignableTo <AsyncPropertyValidator <string> >();
        }