public void ReferenceTypesDontGetImplicitRequiredAttribute()
        {
            // Arrange
            var provider = new DataAnnotations4ModelValidatorProvider();
            var context  = new ControllerContext();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(string));

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(metadata, context);

            // Assert
            Assert.AreEqual(0, validators.Count());
        }
        public void IValidatableObjectGetsAValidator()
        {
            // Arrange
            var provider        = new DataAnnotations4ModelValidatorProvider();
            var mockValidatable = new Mock <IValidatableObject>();
            var context         = new ControllerContext();
            var metadata        = ModelMetadataProviders.Current.GetMetadataForType(() => null, mockValidatable.Object.GetType());

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(metadata, context);

            // Assert
            Assert.AreEqual(1, validators.Count());
        }
        public void NonNullableValueTypeDoesntGetImplicitRequiredAttributeWhenFlagIsOff()
        {
            // Arrange
            var provider = new DataAnnotations4ModelValidatorProvider {
                AddImpliedRequiredAttributes = false
            };
            var context  = new ControllerContext();
            var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(DummyRequiredAttributeHelperClass), "WithoutAttribute");

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(metadata, context);

            // Assert
            Assert.AreEqual(0, validators.Count());
        }
        public void UnknownValidationAttributeGetsDefaultAdapter()
        {
            // Arrange
            var provider = new DataAnnotations4ModelValidatorProvider();
            var context  = new ControllerContext();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(DummyClassWithDummyValidationAttribute));

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(metadata, context);

            // Assert
            var validator = validators.Single();

            Assert.AreEqual(typeof(DataAnnotations4ModelValidator), validator.GetType());
        }
        public void NonNullableValueTypesGetImplicitRequiredAttribute()
        {
            // Arrange
            var provider = new DataAnnotations4ModelValidatorProvider();
            var context  = new ControllerContext();
            var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(DummyRequiredAttributeHelperClass), "WithoutAttribute");

            // Act
            IEnumerable <ModelValidator> validators = provider.GetValidators(metadata, context);

            // Assert
            ModelValidator            validator = validators.Single();
            ModelClientValidationRule rule      = validator.GetClientValidationRules().Single();

            Assert.AreEqual(typeof(ModelClientValidationRequiredRule), rule.GetType());
        }