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"); }
public async Task For_Validate_When_RootValidatorHasSetUpSyncPropertyValidatorAndValidObjectIsPassed_Then_ValidationErrorsAreReturned() { // Arrange: var rootAsyncValidator = new RootAsyncValidatorImplementation(); Expression <Func <RootValidatorsTestsCommon.TestClass, string> > expression = obj => obj.SomeProperty; rootAsyncValidator.Setup(x => x.SomeProperty) .AddRule(x => 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); }
For_Validate_When_RootValidatorHasSetUpBothSyncAndAsyncPropertyValidatorAndValidObjectIsPassed_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"); rootAsyncValidator.Setup(x => x.OtherProperty) .AddRule(x => x == "x", "must be x", "MUST_BE_X"); var toValidate = (object)new RootValidatorsTestsCommon.TestClass { SomeProperty = null, OtherProperty = "y" }; // Act: var validationErrors = await rootAsyncValidator.Validate(toValidate); // Assert: validationErrors.Count.Should().Be(2); 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); validationErrors[1].ErrorMessage.Should().Be("must be x"); validationErrors[1].ErrorCode.Should().Be("MUST_BE_X"); validationErrors[1].PropertyPath.IsEmpty.Should().Be(false); validationErrors[1].ProvidedValue.Should().Be("y"); }