コード例 #1
0
        public void GetValidatorsReturnsNothingForValidModel()
        {
            // Arrange
            var validatorProvider = new InvalidModelValidatorProvider();

            // Act
            var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(ValidModel)));

            // Assert
            Assert.Empty(validators);
        }
コード例 #2
0
        public void GetValidatorsReturnsNothingForValidModel()
        {
            // Arrange
            var validatorProvider = new InvalidModelValidatorProvider();

            // Act
            var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(ValidModel)));

            // Assert
            Assert.Empty(validators);
        }
コード例 #3
0
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelProperty()
        {
            // Arrange
            var name = typeof(InvalidModel).FullName;
            var validatorProvider = new InvalidModelValidatorProvider();

            // Act
            var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForProperty(null, typeof(InvalidModel), "Value"));

            // Assert
            Assert.Equal(1, validators.Count());
            ExceptionAssert.Throws<InvalidOperationException>(() => validators.First().Validate(null),
                "Property 'Value' on type '" + name + "' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)].");
        }
コード例 #4
0
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelProperty()
        {
            // Arrange
            var name = typeof(InvalidModel).FullName;
            var validatorProvider = new InvalidModelValidatorProvider();

            // Act
            var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForProperty(null, typeof(InvalidModel), "Value"));

            // Assert
            Assert.Equal(1, validators.Count());
            ExceptionAssert.Throws <InvalidOperationException>(() => validators.First().Validate(null),
                                                               "Property 'Value' on type '" + name + "' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)].");
        }
コード例 #5
0
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelType()
        {
            // Arrange
            var name = typeof(InvalidModel).FullName;
            var validatorProvider = new InvalidModelValidatorProvider();

            // Act
            var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(InvalidModel)));

            // Assert
            Assert.Equal(2, validators.Count());
            ExceptionAssert.Throws<InvalidOperationException>(() => validators.ElementAt(0).Validate(null),
                "Non-public property 'Internal' on type '" + name  + "' is attributed with one or more validation attributes. Validation attributes on non-public properties are not supported. Consider using a public property for validation instead.");
            ExceptionAssert.Throws<InvalidOperationException>(() => validators.ElementAt(1).Validate(null),
                "Field 'Field' on type '" + name + "' is attributed with one or more validation attributes. Validation attributes on fields are not supported. Consider using a public property for validation instead.");
        }
コード例 #6
0
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelType()
        {
            // Arrange
            var name = typeof(InvalidModel).FullName;
            var validatorProvider = new InvalidModelValidatorProvider();

            // Act
            var validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(InvalidModel)));

            // Assert
            Assert.Equal(2, validators.Count());
            ExceptionAssert.Throws <InvalidOperationException>(() => validators.ElementAt(0).Validate(null),
                                                               "Non-public property 'Internal' on type '" + name + "' is attributed with one or more validation attributes. Validation attributes on non-public properties are not supported. Consider using a public property for validation instead.");
            ExceptionAssert.Throws <InvalidOperationException>(() => validators.ElementAt(1).Validate(null),
                                                               "Field 'Field' on type '" + name + "' is attributed with one or more validation attributes. Validation attributes on fields are not supported. Consider using a public property for validation instead.");
        }