Esempio n. 1
0
        public void PredicateFalseAndRequiredFieldPopulated_Returns_True()
        {
            // Arrange
            var dto = new TestDto
            {
                PredicateProperty = false,
                TestField         = "something"
            };
            var serviceProvider = new Mock <IServiceProvider>();
            var sut             = new RequiredIfAttributesMatch(nameof(TestDto.PredicateProperty), true);

            serviceProvider.Setup(context => context.GetService(It.IsAny <Type>())).Returns(dto);

            // Act
            var result = sut.GetValidationResult(dto.TestField,
                                                 new ValidationContext(dto, serviceProvider.Object, new Dictionary <object, object>()));

            // Assert
            Assert.Equal(ValidationResult.Success, result);
        }
Esempio n. 2
0
        public void PredicateFinalAndRequiredFieldPopulated_Returns_True()
        {
            // Arrange
            var dto = new TestDtoTdo
            {
                Status    = true,
                TestField = null
            };
            var serviceProvider = new Mock <IServiceProvider>().Object;

            serviceProvider.GetService(dto.GetType());
            var validationContext = new ValidationContext(dto, serviceProvider, new Dictionary <object, object>());
            var sut = new RequiredIfAttributesMatch(nameof(TestDtoTdo.Status), true);

            // Act
            var result = sut.GetValidationResult(dto.TestField, validationContext);

            // Assert
            Assert.Equal("The TestDtoTdo field is required.", result?.ErrorMessage);
        }
Esempio n. 3
0
        public void PredicateTrueAndRequiredFieldNull_Returns_False()
        {
            // Arrange
            var dto = new TestDto
            {
                PredicateProperty = true,
                TestField         = null
            };
            var serviceProvider = new Mock <IServiceProvider>();
            var sut             = new RequiredIfAttributesMatch(nameof(TestDto.PredicateProperty), true);

            serviceProvider.Setup(context => context.GetService(It.IsAny <Type>())).Returns(dto);

            // Act
            var result = sut.GetValidationResult(
                dto.TestField,
                new ValidationContext(dto, serviceProvider.Object, new Dictionary <object, object>()));

            // Assert
            Assert.Equal("The TestDto field is required.", result?.ErrorMessage);
        }
Esempio n. 4
0
        public void PredicateTrueAndRequiredFieldPopulated_Returns_True()
        {
            // Arrange
            var dto = new TestDto
            {
                PredicateProperty = true,
                TestField         = "bob"
            };
            var serviceProvider = new Mock <IServiceProvider>().Object;

            serviceProvider.GetService(dto.GetType());
            var validationContext = new ValidationContext(dto, serviceProvider, new Dictionary <object, object>());
            var sut            = new RequiredIfAttributesMatch(nameof(TestDto.PredicateProperty), true);
            var expectedResult = ValidationResult.Success;

            // Act
            var result = sut.GetValidationResult(dto.TestField, validationContext);

            // Assert
            Assert.Equal(expectedResult, result);
        }