public void ShouldProcessTheDefaultValidatorForNotNullableValueTypesWhenFormValueIsNull()
        {
            ControllerContext controllerContext = new ControllerContext {
                HttpContext = Substitute.For <HttpContextBase>()
            };
            IValidatorFactory validatorFactory = Substitute.For <IValidatorFactory>();

            validatorFactory.GetValidatorForOptional(typeof(TestModel)).Returns(x => new TestModelValidator());

            LaboModelValidatorProvider.Configure(validatorFactory);

            FormCollection form = new FormCollection
            {
                { "Age", string.Empty }
            };

            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelName             = "test",
                ModelMetadata         = GetModelMetaData(typeof(TestModel)),
                ModelState            = new ModelStateDictionary(),
                FallbackToEmptyPrefix = true,
                ValueProvider         = form.ToValueProvider()
            };

            DefaultModelBinder binder = new DefaultModelBinder();

            binder.BindModel(controllerContext, bindingContext);

            Assert.AreEqual("'Age' must not be empty.", bindingContext.ModelState["Age"].Errors.Single().ErrorMessage);
        }
        public void GetValidators_ShouldReturnValidatorsWithTheSamePropertiesAsTheSpecifiedProperty()
        {
            IValidatorFactory validatorFactory = Substitute.For <IValidatorFactory>();

            validatorFactory.GetValidatorForOptional(typeof(TestModel)).Returns(x => new TestModelValidator());

            ControllerContext controllerContext = new ControllerContext {
                HttpContext = Substitute.For <HttpContextBase>()
            };
            LaboModelValidatorProvider provider = new LaboModelValidatorProvider(validatorFactory);

            ValidateValidatorPropertyNames(x => x.Name, provider, controllerContext);
            ValidateValidatorPropertyNames(x => x.Email, provider, controllerContext);
        }
        public void GetValidators_ShouldReturnLaboPropertyValidatorWhenTheSpecifiedValidatorTypeIsNotInThePropertyValidatorFactories()
        {
            IValidatorFactory validatorFactory = Substitute.For <IValidatorFactory>();

            validatorFactory.GetValidatorForOptional(typeof(TestModel)).Returns(x => new TestModelValidator());

            ControllerContext controllerContext = new ControllerContext {
                HttpContext = Substitute.For <HttpContextBase>()
            };
            LaboModelValidatorProvider provider = new LaboModelValidatorProvider(validatorFactory)
            {
                AddImplicitRequiredAttributeForValueTypes = false
            };

            string                 propertyName      = LinqUtils.GetMemberName <TestModel, string>(x => x.Type);
            ModelMetadata          propertyMetaData  = GetModelMetaDataForProperty(typeof(TestModel), propertyName);
            IList <ModelValidator> properyValidators = provider.GetValidators(propertyMetaData, controllerContext).ToList();

            Assert.IsTrue(properyValidators.First() is LaboPropertyValidator);
        }
        public void GetValidators_ShouldNotReturnNotNullValidatorWhenModelMetadataIsRequiredAndAddImplicitRequiredAttributeForValueTypesIsFalse()
        {
            IValidatorFactory validatorFactory = Substitute.For <IValidatorFactory>();

            validatorFactory.GetValidatorForOptional(typeof(TestModel)).Returns(x => new TestModelValidator());

            ControllerContext controllerContext = new ControllerContext {
                HttpContext = Substitute.For <HttpContextBase>()
            };
            LaboModelValidatorProvider provider = new LaboModelValidatorProvider(validatorFactory)
            {
                AddImplicitRequiredAttributeForValueTypes = false
            };

            string                 propertyName      = LinqUtils.GetMemberName <TestModel, int>(x => x.Age);
            ModelMetadata          propertyMetaData  = GetModelMetaDataForProperty(typeof(TestModel), propertyName);
            IList <ModelValidator> properyValidators = provider.GetValidators(propertyMetaData, controllerContext).ToList();

            Assert.AreEqual(0, properyValidators.Count);
        }
        public void GetValidators_ShouldReturnTheRightValidators()
        {
            IValidatorFactory validatorFactory = Substitute.For <IValidatorFactory>();

            validatorFactory.GetValidatorForOptional(typeof(TestModel)).Returns(x => new TestModelValidator());

            ControllerContext controllerContext = new ControllerContext {
                HttpContext = Substitute.For <HttpContextBase>()
            };
            LaboModelValidatorProvider provider = new LaboModelValidatorProvider(validatorFactory);

            string                 propertyName      = LinqUtils.GetMemberName <TestModel, string>(x => x.Name);
            ModelMetadata          propertyMetaData  = GetModelMetaDataForProperty(typeof(TestModel), propertyName);
            IList <ModelValidator> properyValidators = provider.GetValidators(propertyMetaData, controllerContext).ToList();

            Assert.AreEqual(2, properyValidators.Count);
            Assert.IsTrue(properyValidators[0].IsRequired);
            Assert.IsTrue(GetInnerValidator(properyValidators[0]) is NotNullValidator);
            Assert.IsTrue(GetInnerValidator(properyValidators[1]) is LengthValidator);
            Assert.AreEqual(10, ((LengthValidator)GetInnerValidator(properyValidators[1])).Max);
        }