public void ValidateSetsDisplayNamePropertyOfValidationContextAsExpected( ModelMetadata metadata, string expectedDisplayName) { // 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(expectedDisplayName, context.DisplayName)) .Returns(ValidationResult.Success) .Verifiable(); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator( _noValidatorProviders, attribute.Object); // Act IEnumerable <ModelValidationResult> results = validator.Validate(metadata, container: null); // Assert Assert.Empty(results); attribute.VerifyAll(); }
private static void ValidateSetsMemberNameProperty_NotUsingDisplayName_Inner() { // Arrange DataAnnotationsModelValidator.UseLegacyValidationMemberName = false; var expectedMemberName = "Name"; 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(_noValidatorProviders, attribute.Object); var metadata = _metadataProvider.GetMetadataForProperty(() => string.Empty, typeof(AnnotatedModel), "Name"); // Act var results = validator.Validate(metadata, container: null); // Assert Assert.Empty(results); attribute.VerifyAll(); }
public void ValidateReturnsMemberNameIfItIsDifferentFromDisplayName() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForType( () => new SampleModel(), typeof(SampleModel) ); Mock <ValidationAttribute> attribute = new Mock <ValidationAttribute> { CallBase = true }; attribute .Protected() .Setup <ValidationResult>( "IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>() ) .Returns(new ValidationResult("Name error", new[] { "Name" })); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator( _noValidatorProviders, attribute.Object ); // Act IEnumerable <ModelValidationResult> results = validator.Validate( metadata, container: null ); // Assert ModelValidationResult validationResult = Assert.Single(results); Assert.Equal("Name", validationResult.MemberName); }
public void ValidatateWithValidationResultSuccess() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForProperty( () => 15, typeof(string), "Length" ); Mock <ValidationAttribute> attribute = new Mock <ValidationAttribute> { CallBase = true }; attribute .Protected() .Setup <ValidationResult>( "IsValid", ItExpr.IsAny <object>(), ItExpr.IsAny <ValidationContext>() ) .Returns(ValidationResult.Success); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator( _noValidatorProviders, attribute.Object ); // Act IEnumerable <ModelValidationResult> result = validator.Validate(metadata, null); // Assert Assert.Empty(result); }
public void ValidateWithIsValidFalse() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForProperty( () => 15, typeof(string), "Length" ); Mock <ValidationAttribute> attribute = new Mock <ValidationAttribute> { CallBase = true }; attribute.Setup(a => a.IsValid(metadata.Model)).Returns(false); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator( _noValidatorProviders, attribute.Object ); // Act IEnumerable <ModelValidationResult> result = validator.Validate(metadata, null); // Assert var validationResult = result.Single(); Assert.Equal("", validationResult.MemberName); Assert.Equal(attribute.Object.FormatErrorMessage("Length"), validationResult.Message); }
public void ValidateWithIsValidTrue() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length"); Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true }; attribute.Setup(a => a.IsValid(metadata.Model)).Returns(true); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> result = validator.Validate(metadata, null); // Assert Assert.Empty(result); }
public void ValidateWithIsValidFalse() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length"); Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true }; attribute.Setup(a => a.IsValid(metadata.Model)).Returns(false); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> result = validator.Validate(metadata, null); // Assert var validationResult = result.Single(); Assert.Equal("", validationResult.MemberName); Assert.Equal(attribute.Object.FormatErrorMessage("Length"), validationResult.Message); }
public void ValidateWithIsValidTrue() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length"); Mock <ValidationAttribute> attribute = new Mock <ValidationAttribute> { CallBase = true }; attribute.Setup(a => a.IsValid(metadata.Model)).Returns(true); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object); // Act IEnumerable <ModelValidationResult> result = validator.Validate(metadata, null); // Assert Assert.Empty(result); }
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(); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> results = validator.Validate(metadata, container: null); // Assert Assert.Empty(results); attribute.VerifyAll(); }
public void ValidateReturnsSingleValidationResultIfMemberNameSequenceIsEmpty() { // Arrange const string errorMessage = "Some error message"; ModelMetadata metadata = _metadataProvider.GetMetadataForProperty( () => 15, typeof(string), "Length" ); Mock <ValidationAttribute> 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( _noValidatorProviders, attribute.Object ); // Act IEnumerable <ModelValidationResult> results = validator.Validate( metadata, container: null ); // Assert ModelValidationResult validationResult = Assert.Single(results); Assert.Equal(errorMessage, validationResult.Message); Assert.Empty(validationResult.MemberName); }
public void ValidateReturnsSingleValidationResultIfOneMemberNameIsSpecified() { // Arrange const string errorMessage = "A different error message"; ModelMetadata metadata = _metadataProvider.GetMetadataForType(() => new object(), typeof(object)); Mock <ValidationAttribute> 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(_noValidatorProviders, attribute.Object); // Act IEnumerable <ModelValidationResult> results = validator.Validate(metadata, container: null); // Assert ModelValidationResult validationResult = Assert.Single(results); Assert.Equal(errorMessage, validationResult.Message); Assert.Equal("FirstName", validationResult.MemberName); }
public void ValidateReturnsMemberNameIfItIsDifferentFromDisplayName() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForType(() => new SampleModel(), typeof(SampleModel)); Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true }; attribute.Protected() .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>()) .Returns(new ValidationResult("Name error", new[] { "Name" })); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> results = validator.Validate(metadata, container: null); // Assert ModelValidationResult validationResult = Assert.Single(results); Assert.Equal("Name", validationResult.MemberName); }
public void ValidateReturnsSingleValidationResultIfOneMemberNameIsSpecified() { // Arrange const string errorMessage = "A different error message"; ModelMetadata metadata = _metadataProvider.GetMetadataForType(() => new object(), typeof(object)); Mock<ValidationAttribute> 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(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> results = validator.Validate(metadata, container: null); // 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"; ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length"); Mock<ValidationAttribute> 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(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> results = validator.Validate(metadata, container: null); // Assert ModelValidationResult validationResult = Assert.Single(results); Assert.Equal(errorMessage, validationResult.Message); Assert.Empty(validationResult.MemberName); }
public void ValidatateWithValidationResultSuccess() { // Arrange ModelMetadata metadata = _metadataProvider.GetMetadataForProperty(() => 15, typeof(string), "Length"); Mock<ValidationAttribute> attribute = new Mock<ValidationAttribute> { CallBase = true }; attribute.Protected() .Setup<ValidationResult>("IsValid", ItExpr.IsAny<object>(), ItExpr.IsAny<ValidationContext>()) .Returns(ValidationResult.Success); DataAnnotationsModelValidator validator = new DataAnnotationsModelValidator(_noValidatorProviders, attribute.Object); // Act IEnumerable<ModelValidationResult> result = validator.Validate(metadata, null); // Assert Assert.Empty(result); }