public void AssertClientValidationRules() { ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; string propertyName = LinqUtils.GetMemberName <TestModel, string>(x => x.Name); ModelMetadata propertyMetaData = GetModelMetaDataForProperty(typeof(TestModel), propertyName, () => new TestModel()); LaboPropertyValidator laboPropertyValidator = CreateLaboPropertyValidator(propertyMetaData, controllerContext, CreateEntityValidationRule()); IList <ModelClientValidationRule> modelClientValidationRules = laboPropertyValidator.GetClientValidationRules().ToList(); ValidateClientValidationRules(laboPropertyValidator, modelClientValidationRules); }
public void ShouldValidateShouldBeTrueWhenTheSpecifiedPropertyIsNotNullableValueTypeAndModelIsNull() { ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; string propertyName = LinqUtils.GetMemberName <TestModel, int>(x => x.Age); ModelMetadata propertyMetaData = GetModelMetaDataForProperty(typeof(TestModel), propertyName, null); IEntityValidationRule <TestModel> entityValidationRule = new TestModelValidator().EntityValidationRules[0]; LaboPropertyValidator laboPropertyValidator = CreateLaboPropertyValidator(propertyMetaData, controllerContext, entityValidationRule); Assert.IsTrue(laboPropertyValidator.ShouldValidate); }
public void ValidateShouldReturnEmptyValidationResultWhenShouldValidateIsSetToFalse() { ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; string propertyName = LinqUtils.GetMemberName <TestModel, string>(x => x.Name); ModelMetadata propertyMetaData = GetModelMetaDataForProperty(typeof(TestModel), propertyName, () => new TestModel()); IEntityValidationRule <TestModel> entityValidationRule = new TestModelValidator().EntityValidationRules[0]; LaboPropertyValidator laboPropertyValidator = new LaboPropertyValidator(propertyMetaData, controllerContext, entityValidationRule, false); IEnumerable <ModelValidationResult> modelValidationResults = laboPropertyValidator.Validate(null); Assert.AreEqual(0, modelValidationResults.Count()); }
public void GetClientValidationRulesShouldReturnEmptyRulesWhenValidatorDoesNotImplementIClientValidatable() { ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; string propertyName = LinqUtils.GetMemberName <TestModel, string>(x => x.Test); ModelMetadata propertyMetaData = GetModelMetaDataForProperty(typeof(TestModel), propertyName, () => new TestModel()); IEntityValidationRule <TestModel> entityValidationRule = new TestModelValidator().EntityValidationRules[0]; LaboPropertyValidator laboPropertyValidator = new LaboPropertyValidator(propertyMetaData, controllerContext, entityValidationRule); IEnumerable <ModelClientValidationRule> clientValidationRules = laboPropertyValidator.GetClientValidationRules(); Assert.AreEqual(0, clientValidationRules.Count()); }
public void ValidateShouldReturnValidationMessageWhenShouldValidateIsSetToTrue() { ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; string propertyName = LinqUtils.GetMemberName <TestModel, string>(x => x.Name); ModelMetadata propertyMetaData = GetModelMetaDataForProperty(typeof(TestModel), propertyName, () => new TestModel()); IEntityValidationRule <TestModel> entityValidationRule = new TestModelValidator().EntityValidationRules[0]; LaboPropertyValidator laboPropertyValidator = new LaboPropertyValidator(propertyMetaData, controllerContext, entityValidationRule, true); ModelValidationResult modelValidationResult = laboPropertyValidator.Validate(null).First(); Assert.AreEqual("'Name' must not be empty.", modelValidationResult.Message); Assert.AreEqual("Name", modelValidationResult.MemberName); }
public void GetClientValidationRulesShouldReturnIClientValidatableGetClientValidationRulesWhenValidatorImplementsIClientValidatable() { ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; string propertyName = LinqUtils.GetMemberName <TestModel, string>(x => x.Test); ModelMetadata propertyMetaData = GetModelMetaDataForProperty(typeof(TestModel), propertyName, () => new TestModel()); IEntityValidationRule <TestModel> entityValidationRule = new TestModelValidator().EntityValidationRules[1]; LaboPropertyValidator laboPropertyValidator = new LaboPropertyValidator(propertyMetaData, controllerContext, entityValidationRule); ModelClientValidationRule clientValidationRule = laboPropertyValidator.GetClientValidationRules().First(); Assert.AreEqual("Error Message", clientValidationRule.ErrorMessage); Assert.AreEqual("email", clientValidationRule.ValidationType); }
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 ValidateMustReturnInvalidValidationResultWhenTheSpecifiedTheValidatorReturnsFalse() { const string firstName = "Foo"; IValidator validator = Substitute.For <IValidator>(); validator.IsValid(null).ReturnsForAnyArgs(false); EntityPropertyValidationRule <Customer, string> entityPropertyValidationRule = new EntityPropertyValidationRule <Customer, string>(new EntityPropertyValidator(validator), Substitute.For <IPropertyDisplayNameResolver>(), x => x.FirstName); ValidationResult validationResult = entityPropertyValidationRule.Validate(new Customer { FirstName = firstName }); validator.Received(1).IsValid(firstName); Assert.AreEqual(1, validationResult.Errors.Count); Assert.AreEqual(LinqUtils.GetMemberName <Customer, string>(x => x.FirstName), validationResult.Errors[0].PropertyName); Assert.IsFalse(validationResult.IsValid); }
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); }
public void GetValidators_ValidatorFactoryGetValidatorForOptionalContainerTypeShouldBeCalled() { IValidatorFactory validatorFactory = Substitute.For <IValidatorFactory>(); ControllerContext controllerContext = new ControllerContext { HttpContext = Substitute.For <HttpContextBase>() }; LaboModelValidatorProvider provider = new LaboModelValidatorProvider(validatorFactory); ModelMetadata modelMetaData = GetModelMetaDataForProperty(typeof(TestModel), LinqUtils.GetMemberName <TestModel, string>(x => x.Name)); provider.GetValidators(modelMetaData, controllerContext); validatorFactory.Received(1).GetValidatorForOptional(modelMetaData.ContainerType); }