Ejemplo n.º 1
0
        public void GetValidatorsReturnsNothingForValidModel()
        {
            InvalidModelValidatorProvider validatorProvider = new InvalidModelValidatorProvider();

            IEnumerable <ModelValidator> validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(ValidModel)), _noValidatorProviders);

            Assert.Empty(validators);
        }
        public void GetValidatorsReturnsNothingForValidModel()
        {
            InvalidModelValidatorProvider validatorProvider = new InvalidModelValidatorProvider();

            IEnumerable<ModelValidator> validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(ValidModel)), _noValidatorProviders);

            Assert.Empty(validators);
        }
Ejemplo n.º 3
0
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelProperty()
        {
            InvalidModelValidatorProvider validatorProvider = new InvalidModelValidatorProvider();

            IEnumerable <ModelValidator> validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForProperty(null, typeof(InvalidModel), "Value"), _noValidatorProviders);

            Assert.Equal(1, validators.Count());
            Assert.Throws <InvalidOperationException>(() => validators.First().Validate(null, null),
                                                      "Property 'Value' on type 'System.Web.Http.Validation.Providers.InvalidModelValidatorProviderTest+InvalidModel' 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)].");
        }
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelProperty()
        {
            InvalidModelValidatorProvider validatorProvider = new InvalidModelValidatorProvider();

            IEnumerable<ModelValidator> validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForProperty(null, typeof(InvalidModel), "Value"), _noValidatorProviders);

            Assert.Equal(1, validators.Count());
            Assert.Throws<InvalidOperationException>(() => validators.First().Validate(null, null),
                "Property 'Value' on type 'System.Web.Http.Validation.Providers.InvalidModelValidatorProviderTest+InvalidModel' 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)].");
        }
Ejemplo n.º 5
0
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelType()
        {
            InvalidModelValidatorProvider validatorProvider = new InvalidModelValidatorProvider();

            IEnumerable <ModelValidator> validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(InvalidModel)), _noValidatorProviders);

            Assert.Equal(2, validators.Count());
            Assert.Throws <InvalidOperationException>(() => validators.ElementAt(0).Validate(null, null),
                                                      "Non-public property 'Internal' on type 'System.Web.Http.Validation.Providers.InvalidModelValidatorProviderTest+InvalidModel' 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.");
            Assert.Throws <InvalidOperationException>(() => validators.ElementAt(1).Validate(null, null),
                                                      "Field 'Field' on type 'System.Web.Http.Validation.Providers.InvalidModelValidatorProviderTest+InvalidModel' is attributed with one or more validation attributes. Validation attributes on fields are not supported. Consider using a public property for validation instead.");
        }
        public void GetValidatorsReturnsInvalidModelValidatorsForInvalidModelType()
        {
            InvalidModelValidatorProvider validatorProvider = new InvalidModelValidatorProvider();

            IEnumerable<ModelValidator> validators = validatorProvider.GetValidators(_metadataProvider.GetMetadataForType(null, typeof(InvalidModel)), _noValidatorProviders);

            Assert.Equal(2, validators.Count());
            Assert.Throws<InvalidOperationException>(() => validators.ElementAt(0).Validate(null, null),
                "Non-public property 'Internal' on type 'System.Web.Http.Validation.Providers.InvalidModelValidatorProviderTest+InvalidModel' 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.");
            Assert.Throws<InvalidOperationException>(() => validators.ElementAt(1).Validate(null, null),
                "Field 'Field' on type 'System.Web.Http.Validation.Providers.InvalidModelValidatorProviderTest+InvalidModel' is attributed with one or more validation attributes. Validation attributes on fields are not supported. Consider using a public property for validation instead.");
        }