Example #1
0
        public void GetValidators_should_return_validators_for_item_in_list_and_validators_for_itself()
        {
            // Arrange
            var model = new ParentClass
            {
                Children = new List<NestedClass>
                {
                    new NestedClass(),
                    new NestedClass()
                }
            };

            // Action
            var metaData = new ModelMetadata(ModelMetadataProviders.Current,
                                             typeof(ParentClass),
                                             () => model.Children,
                                             typeof(List<NestedClass>),
                                             "Children");

            var validators = metaData.GetValidators().ToList();

            // Assert
            Assert.AreEqual(3, validators.Count());
        }
Example #2
0
        public void Properties_should_return_a_single_metadata_for_a_enumerable_property()
        {
            // Arrange
            var model = new ParentClass
            {
                Children = new List<NestedClass>
                {
                    new NestedClass(),
                    new NestedClass()
                }
            };

            // Action
            var properties = ModelMetadataProviders.Current.GetMetadataForType(() => model, typeof(ParentClass)).Properties.ToList();

            // Assert
            Assert.AreEqual(1, properties.Count);
            Assert.AreEqual("ParentClass.Children", properties[0].FullName);
        }
Example #3
0
        public void Validate_can_validate_nested_item_in_list()
        {
            // Arrange
            var model = new ParentClass
            {
                Children = new List<NestedClass>
                {
                    new NestedClass
                    {
                        Name = "Van Thoai Nguyen"
                    }
                }
            };

            // Action
            var metaData = new ModelMetadata(ModelMetadataProviders.Current,
                                             null,
                                             () => model,
                                             typeof(ParentClass),
                                             null);

            var validator = ModelValidator.GetModelValidator(metaData);
            var result = validator.Validate(model).ToList();

            // Assert
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("The field Name must be a string with a maximum length of 5.", result[0].Message);
            Assert.AreEqual("ParentClass.Children[0].Name", result[0].MemberName);
        }