Esempio n. 1
0
        public void Validate_SkipsRemainingValidationIfModelStateIsInvalid()
        {
            // Because a property validator fails, the model validator shouldn't run

            // Arrange
            var expected = new[]
            {
                "In OnValidating()",
                "In IValidatableObject.Validate()",
                "In OnValidated()"
            };
            var log           = new List <string>();
            var model         = new LoggingValidatableObject(log);
            var modelMetadata = GetModelMetadata(model);
            var provider      = new EmptyModelMetadataProvider();
            var childMetadata = provider.GetMetadataForProperty(() => model,
                                                                model.GetType(),
                                                                "InvalidStringProperty");
            var node = new ModelValidationNode(modelMetadata, "theKey");

            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.InvalidStringProperty"));
            node.Validating += (sender, e) => log.Add("In OnValidating()");
            node.Validated  += (sender, e) => log.Add("In OnValidated()");
            var context = CreateContext(modelMetadata);

            // Act
            node.Validate(context);

            // Assert
            Assert.Equal(expected, log);
            Assert.Equal("Sample error message",
                         context.ModelState["theKey.InvalidStringProperty"].Errors[0].ErrorMessage);
        }
Esempio n. 2
0
        public void Validate_Ordering()
        {
            // Proper order of invocation:
            // 1. OnValidating()
            // 2. Child validators
            // 3. This validator
            // 4. OnValidated()

            // Arrange
            var expected = new[]
            {
                "In OnValidating()",
                "In LoggingValidatonAttribute.IsValid()",
                "In IValidatableObject.Validate()",
                "In OnValidated()"
            };
            var log           = new List <string>();
            var model         = new LoggingValidatableObject(log);
            var modelMetadata = GetModelMetadata(model);
            var provider      = new EmptyModelMetadataProvider();
            var childMetadata = provider.GetMetadataForProperty(() => model, model.GetType(), "ValidStringProperty");
            var node          = new ModelValidationNode(modelMetadata, "theKey");

            node.Validating += (sender, e) => log.Add("In OnValidating()");
            node.Validated  += (sender, e) => log.Add("In OnValidated()");
            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.ValidStringProperty"));
            var context = CreateContext(modelMetadata);

            // Act
            node.Validate(context);

            // Assert
            Assert.Equal(expected, log);
        }
Esempio n. 3
0
        public void Validate_Ordering()
        {
            // Proper order of invocation:
            // 1. OnValidating()
            // 2. Child validators
            // 3. This validator
            // 4. OnValidated()

            // Arrange
            List <string>            log           = new List <string>();
            LoggingValidatableObject model         = new LoggingValidatableObject(log);
            ModelMetadata            modelMetadata = GetModelMetadata(model);
            ModelMetadata            childMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(
                () => model,
                model.GetType(),
                "ValidStringProperty"
                );
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey");

            node.Validating += (sender, e) => log.Add("In OnValidating()");
            node.Validated  += (sender, e) => log.Add("In OnValidated()");
            node.ChildNodes.Add(
                new ModelValidationNode(childMetadata, "theKey.ValidStringProperty")
                );

            // Act
            node.Validate(ContextUtil.CreateActionContext());

            // Assert
            Assert.Equal(
                new[]
            {
                "In OnValidating()",
                "In LoggingValidatonAttribute.IsValid()",
                "In IValidatableObject.Validate()",
                "In OnValidated()"
            },
                log.ToArray()
                );
        }
        public void Validate_SkipsRemainingValidationIfModelStateIsInvalid()
        {
            // Because a property validator fails, the model validator shouldn't run

            // Arrange
            List<string> log = new List<string>();
            LoggingValidatableObject model = new LoggingValidatableObject(log);
            ModelMetadata modelMetadata = GetModelMetadata(model);
            ModelMetadata childMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(() => model, model.GetType(), "InvalidStringProperty");
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey");
            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.InvalidStringProperty"));
            node.Validating += (sender, e) => log.Add("In OnValidating()");
            node.Validated += (sender, e) => log.Add("In OnValidated()");
            HttpActionContext context = ContextUtil.CreateActionContext();

            // Act
            node.Validate(context);

            // Assert
            Assert.Equal(new[] { "In OnValidating()", "In IValidatableObject.Validate()", "In OnValidated()" }, log.ToArray());
            Assert.Equal("Sample error message", context.ModelState["theKey.InvalidStringProperty"].Errors[0].ErrorMessage);
        }
        public void Validate_Ordering()
        {
            // Proper order of invocation:
            // 1. OnValidating()
            // 2. Child validators
            // 3. This validator
            // 4. OnValidated()

            // Arrange
            List<string> log = new List<string>();
            LoggingValidatableObject model = new LoggingValidatableObject(log);
            ModelMetadata modelMetadata = GetModelMetadata(model);
            ModelMetadata childMetadata = new EmptyModelMetadataProvider().GetMetadataForProperty(() => model, model.GetType(), "ValidStringProperty");
            ModelValidationNode node = new ModelValidationNode(modelMetadata, "theKey");
            node.Validating += (sender, e) => log.Add("In OnValidating()");
            node.Validated += (sender, e) => log.Add("In OnValidated()");
            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.ValidStringProperty"));

            // Act
            node.Validate(ContextUtil.CreateActionContext());

            // Assert
            Assert.Equal(new[] { "In OnValidating()", "In LoggingValidatonAttribute.IsValid()", "In IValidatableObject.Validate()", "In OnValidated()" }, log.ToArray());
        }
Esempio n. 6
0
        public void Validate_Ordering()
        {
            // Proper order of invocation:
            // 1. OnValidating()
            // 2. Child validators
            // 3. This validator
            // 4. OnValidated()

            // Arrange
            var expected = new[]
            {
                "In OnValidating()",
                "In LoggingValidatonAttribute.IsValid()",
                "In IValidatableObject.Validate()",
                "In OnValidated()"
            };
            var log = new List<string>();
            var model = new LoggingValidatableObject(log);
            var modelMetadata = GetModelMetadata(model);
            var provider = new EmptyModelMetadataProvider();
            var childMetadata = provider.GetMetadataForProperty(() => model, model.GetType(), "ValidStringProperty");
            var node = new ModelValidationNode(modelMetadata, "theKey");
            node.Validating += (sender, e) => log.Add("In OnValidating()");
            node.Validated += (sender, e) => log.Add("In OnValidated()");
            node.ChildNodes.Add(new ModelValidationNode(childMetadata, "theKey.ValidStringProperty"));
            var context = CreateContext(modelMetadata);

            // Act
            node.Validate(context);

            // Assert
            Assert.Equal(expected, log);
        }