public void ValidateSetsMemberNamePropertyOfValidationContextForProperties(ModelMetadata metadata,
                                                                                   string expectedMemberName)
        {
            // Arrange
            var attribute = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Protected()
            .Setup <ValidationResult>("IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>())
            .Callback((object o, ValidationContext context) =>
            {
                Assert.Equal(expectedMemberName, context.MemberName);
            })
            .Returns(ValidationResult.Success)
            .Verifiable();
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            Assert.Empty(results);
            attribute.VerifyAll();
        }
        public void ValidateWithIsValidTrue()
        {
            // Arrange
            var metadata  = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Setup(a => a.IsValid(metadata.Model)).Returns(true);
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var result = validator.Validate(validationContext);

            // Assert
            Assert.Empty(result);
        }
        public void ValidatateWithValidationResultSuccess()
        {
            // Arrange
            var metadata  = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Protected()
            .Setup <ValidationResult>("IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>())
            .Returns(ValidationResult.Success);
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var result = validator.Validate(validationContext);

            // Assert
            Assert.Empty(result);
        }
        public void ValidateWithIsValidFalse()
        {
            // Arrange
            var metadata  = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Setup(a => a.IsValid(metadata.Model)).Returns(false);
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var result = validator.Validate(validationContext);

            // Assert
            var validationResult = result.Single();

            Assert.Equal("", validationResult.MemberName);
            Assert.Equal(attribute.Object.FormatErrorMessage("Length"), validationResult.Message);
        }
        public void ValidateReturnsMemberNameIfItIsDifferentFromDisplayName()
        {
            // Arrange
            var metadata  = _metadataProvider.GetMetadataForType(() => new SampleModel(), typeof(SampleModel));
            var attribute = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Protected()
            .Setup <ValidationResult>("IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>())
            .Returns(new ValidationResult("Name error", new[] { "Name" }));
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            ModelValidationResult validationResult = Assert.Single(results);

            Assert.Equal("Name", validationResult.MemberName);
        }
        public void ValidateSetsMemberNamePropertyOfValidationContextForProperties(ModelMetadata metadata,
                                                                                   string expectedMemberName)
        {
            // Arrange
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Protected()
                     .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>())
                     .Callback((object o, ValidationContext context) =>
                     {
                         Assert.Equal(expectedMemberName, context.MemberName);
                     })
                     .Returns(ValidationResult.Success)
                     .Verifiable();
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            Assert.Empty(results);
            attribute.VerifyAll();
        }
        public void ValidateReturnsSingleValidationResultIfOneMemberNameIsSpecified()
        {
            // Arrange
            const string errorMessage = "A different error message";
            var          metadata     = _metadataProvider.GetMetadataForType(() => new object(), typeof(object));
            var          attribute    = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Protected()
            .Setup <ValidationResult>("IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>())
            .Returns(new ValidationResult(errorMessage, new[] { "FirstName" }));
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            ModelValidationResult validationResult = Assert.Single(results);

            Assert.Equal(errorMessage, validationResult.Message);
            Assert.Equal("FirstName", validationResult.MemberName);
        }
        public void ValidateReturnsSingleValidationResultIfMemberNameSequenceIsEmpty()
        {
            // Arrange
            const string errorMessage = "Some error message";
            var          metadata     = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var          attribute    = new Mock <ValidationAttribute> {
                CallBase = true
            };

            attribute.Protected()
            .Setup <ValidationResult>("IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>())
            .Returns(new ValidationResult(errorMessage, memberNames: null));
            var validator         = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            ModelValidationResult validationResult = Assert.Single(results);

            Assert.Equal(errorMessage, validationResult.Message);
            Assert.Empty(validationResult.MemberName);
        }
        public void ValidateWithIsValidFalse()
        {
            // Arrange
            var metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Setup(a => a.IsValid(metadata.Model)).Returns(false);
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var result = validator.Validate(validationContext);

            // Assert
            var validationResult = result.Single();
            Assert.Equal("", validationResult.MemberName);
            Assert.Equal(attribute.Object.FormatErrorMessage("Length"), validationResult.Message);
        }
Пример #10
0
        public void ValidateWithIsValidTrue()
        {
            // Arrange
            var metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Setup(a => a.IsValid(metadata.Model)).Returns(true);
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var result = validator.Validate(validationContext);

            // Assert
            Assert.Empty(result);
        }
Пример #11
0
        public void ValidateReturnsMemberNameIfItIsDifferentFromDisplayName()
        {
            // Arrange
            var metadata = _metadataProvider.GetMetadataForType(() => new SampleModel(), typeof(SampleModel));
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Protected()
                     .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>())
                     .Returns(new ValidationResult("Name error", new[] { "Name" }));
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            ModelValidationResult validationResult = Assert.Single(results);
            Assert.Equal("Name", validationResult.MemberName);
        }
Пример #12
0
        public void ValidateReturnsSingleValidationResultIfOneMemberNameIsSpecified()
        {
            // Arrange
            const string errorMessage = "A different error message";
            var metadata = _metadataProvider.GetMetadataForType(() => new object(), typeof(object));
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Protected()
                     .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>())
                     .Returns(new ValidationResult(errorMessage, new[] { "FirstName" }));
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            ModelValidationResult validationResult = Assert.Single(results);
            Assert.Equal(errorMessage, validationResult.Message);
            Assert.Equal("FirstName", validationResult.MemberName);
        }
Пример #13
0
        public void ValidateReturnsSingleValidationResultIfMemberNameSequenceIsEmpty()
        {
            // Arrange
            const string errorMessage = "Some error message";
            var metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Protected()
                     .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>())
                     .Returns(new ValidationResult(errorMessage, memberNames: null));
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var results = validator.Validate(validationContext);

            // Assert
            ModelValidationResult validationResult = Assert.Single(results);
            Assert.Equal(errorMessage, validationResult.Message);
            Assert.Empty(validationResult.MemberName);
        }
Пример #14
0
        public void ValidatateWithValidationResultSuccess()
        {
            // Arrange
            var metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length");
            var attribute = new Mock<ValidationAttribute> { CallBase = true };
            attribute.Protected()
                     .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>())
                     .Returns(ValidationResult.Success);
            var validator = new DataAnnotationsModelValidator(attribute.Object);
            var validationContext = CreateValidationContext(metadata);

            // Act
            var result = validator.Validate(validationContext);

            // Assert
            Assert.Empty(result);
        }