コード例 #1
0
    public void Validate_ReturnsExpectedResults(
        ValidationResult[] innerResults,
        ModelValidationResult[] expectedResults)
    {
        // Arrange
        var adapter = new ValidatableObjectAdapter();
        var model   = new SampleModel();

        foreach (var result in innerResults)
        {
            model.ValidationResults.Add(result);
        }

        var metadata = _metadataProvider.GetMetadataForProperty(
            typeof(SampleModelContainer),
            nameof(SampleModelContainer.SampleModel));
        var validationContext = new ModelValidationContext(
            new ActionContext(),
            metadata,
            _metadataProvider,
            container: null,
            model: model);

        // Act
        var results = adapter.Validate(validationContext);

        // Assert
        Assert.NotNull(results);
        Assert.Equal(expectedResults, results, ModelValidationResultComparer.Instance);
    }
コード例 #2
0
    public void Validate_PassesExpectedNames(
        ModelMetadata metadata,
        string expectedDisplayName,
        string expectedMemberName)
    {
        // Arrange
        var adapter           = new ValidatableObjectAdapter();
        var model             = new SampleModel();
        var validationContext = new ModelValidationContext(
            new ActionContext(),
            metadata,
            _metadataProvider,
            container: new SampleModelContainer(),
            model: model);

        // Act
        var results = adapter.Validate(validationContext);

        // Assert
        Assert.NotNull(results);
        Assert.Empty(results);

        Assert.Equal(expectedDisplayName, model.DisplayName);
        Assert.Equal(expectedMemberName, model.MemberName);
        Assert.Equal(model, model.ObjectInstance);
    }
コード例 #3
0
        public void IValidatableObjectWithMultiPropertyValidationFailure()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(
                () => validatable.Object,
                validatable.Object.GetType()
            );
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable
                .Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(
                    new[]
                    {
                        new ValidationResult("Error Message", new[] { "Property1", "Property2" })
                    }
                )
                .Verifiable();

            // Act
            ModelValidationResult[] results = validator.Validate(null).ToArray();

            // Assert
            validatable.Verify();
            Assert.Equal(2, results.Length);
            Assert.Equal("Error Message", results[0].Message);
            Assert.Equal("Property1", results[0].MemberName);
            Assert.Equal("Error Message", results[1].Message);
            Assert.Equal("Property2", results[1].MemberName);
        }
コード例 #4
0
        public void IValidatableObjectGetsAProperlyPopulatedValidationContext()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(
                () => validatable.Object,
                validatable.Object.GetType()
            );
            var validator = new ValidatableObjectAdapter(metadata, context);
            ValidationContext validationContext = null;
            validatable
                .Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Callback<ValidationContext>(vc => validationContext = vc)
                .Returns(Enumerable.Empty<ValidationResult>())
                .Verifiable();

            // Act
            validator.Validate(null);

            // Assert
            validatable.Verify();
            Assert.Same(validatable.Object, validationContext.ObjectInstance);
            Assert.Null(validationContext.MemberName);
        }
コード例 #5
0
        public void IValidatableObjectWithMultipleModelLevelErrors()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(
                () => validatable.Object,
                validatable.Object.GetType()
            );
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable
                .Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(
                    new ValidationResult[]
                    {
                        new ValidationResult("Error Message 1"),
                        new ValidationResult("Error Message 2")
                    }
                );

            // Act
            ModelValidationResult[] results = validator.Validate(null).ToArray();

            // Assert
            Assert.Equal(2, results.Length);
            Assert.Equal("Error Message 1", results[0].Message);
            Assert.Equal("Error Message 2", results[1].Message);
        }
        public void NonIValidatableObjectInsideMetadataThrows()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => 42, typeof(IValidatableObject));
            var validator = new ValidatableObjectAdapter(metadata, context);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => validator.Validate(null),
                "The model object inside the metadata claimed to be compatible with System.ComponentModel.DataAnnotations.IValidatableObject, but was actually System.Int32.");
        }
        public void IValidatableObjectWhichIsNullReturnsNoErrors()
        {
            // Arrange
            var context   = new ControllerContext();
            var metadata  = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(IValidatableObject));
            var validator = new ValidatableObjectAdapter(metadata, context);

            // Act
            IEnumerable <ModelValidationResult> results = validator.Validate(null);

            // Assert
            Assert.Empty(results);
        }
        public void NonIValidatableObjectInsideMetadataThrows()
        {
            // Arrange
            var context     = new ControllerContext();
            var validatable = new Mock <IValidatableObject>();
            var metadata    = ModelMetadataProviders.Current.GetMetadataForType(() => 42, typeof(IValidatableObject));
            var validator   = new ValidatableObjectAdapter(metadata, context);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => validator.Validate(null),
                "The model object inside the metadata claimed to be compatible with System.ComponentModel.DataAnnotations.IValidatableObject, but was actually System.Int32.");
        }
        public void IValidatableObjectWithNoErrors()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable.Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(Enumerable.Empty<ValidationResult>());

            // Act
            IEnumerable<ModelValidationResult> results = validator.Validate(null);

            // Assert
            Assert.Empty(results);
        }
        public void IValidatableObjectWithNoErrors()
        {
            // Arrange
            var context     = new ControllerContext();
            var validatable = new Mock <IValidatableObject>();
            var metadata    = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator   = new ValidatableObjectAdapter(metadata, context);

            validatable.Setup(vo => vo.Validate(It.IsAny <ValidationContext>()))
            .Returns(Enumerable.Empty <ValidationResult>());

            // Act
            IEnumerable <ModelValidationResult> results = validator.Validate(null);

            // Assert
            Assert.Empty(results);
        }
コード例 #11
0
        public void IValidatableObjectWithModelLevelError()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable.Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(new ValidationResult[] { new ValidationResult("Error Message") });

            // Act
            ModelValidationResult result = validator.Validate(null).Single();

            // Assert
            Assert.Equal("Error Message", result.Message);
            Assert.Equal(String.Empty, result.MemberName);
        }
        public void IValidatableObjectWithModelLevelError()
        {
            // Arrange
            var context     = new ControllerContext();
            var validatable = new Mock <IValidatableObject>();
            var metadata    = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator   = new ValidatableObjectAdapter(metadata, context);

            validatable.Setup(vo => vo.Validate(It.IsAny <ValidationContext>()))
            .Returns(new ValidationResult[] { new ValidationResult("Error Message") });

            // Act
            ModelValidationResult result = validator.Validate(null).Single();

            // Assert
            Assert.Equal("Error Message", result.Message);
            Assert.Equal(String.Empty, result.MemberName);
        }
        public void IValidatableObjectWhichReturnsValidationResultSuccessReturnsNoErrors()
        {
            // Arrange
            var context     = new ControllerContext();
            var validatable = new Mock <IValidatableObject>();
            var metadata    = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator   = new ValidatableObjectAdapter(metadata, context);

            validatable.Setup(vo => vo.Validate(It.IsAny <ValidationContext>()))
            .Returns(new[] { ValidationResult.Success })
            .Verifiable();

            // Act
            ModelValidationResult[] results = validator.Validate(null).ToArray();

            // Assert
            validatable.Verify();
            Assert.Empty(results);
        }
コード例 #14
0
        public void IValidatableObjectGetsAProperlyPopulatedValidationContext()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator = new ValidatableObjectAdapter(metadata, context);
            ValidationContext validationContext = null;
            validatable.Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Callback<ValidationContext>(vc => validationContext = vc)
                .Returns(Enumerable.Empty<ValidationResult>())
                .Verifiable();

            // Act
            validator.Validate(null);

            // Assert
            validatable.Verify();
            Assert.Same(validatable.Object, validationContext.ObjectInstance);
            Assert.Null(validationContext.MemberName);
        }
コード例 #15
0
        public void IValidatableObjectWithMultipleModelLevelErrors()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable.Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(new ValidationResult[]
                {
                    new ValidationResult("Error Message 1"),
                    new ValidationResult("Error Message 2")
                });

            // Act
            ModelValidationResult[] results = validator.Validate(null).ToArray();

            // Assert
            Assert.Equal(2, results.Length);
            Assert.Equal("Error Message 1", results[0].Message);
            Assert.Equal("Error Message 2", results[1].Message);
        }
コード例 #16
0
 public ValidatableObjectAdapterSetup()
 {
     _adapter = new ValidatableObjectAdapter();
 }
コード例 #17
0
        public void IValidatableObjectWhichReturnsValidationResultSuccessReturnsNoErrors()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable.Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(new[] { ValidationResult.Success })
                .Verifiable();

            // Act
            ModelValidationResult[] results = validator.Validate(null).ToArray();

            // Assert
            validatable.Verify();
            Assert.Empty(results);
        }
コード例 #18
0
        public void IValidatableObjectWhichIsNullReturnsNoErrors()
        {
            // Arrange
            var context = new ControllerContext();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(IValidatableObject));
            var validator = new ValidatableObjectAdapter(metadata, context);

            // Act
            IEnumerable<ModelValidationResult> results = validator.Validate(null);

            // Assert
            Assert.Empty(results);
        }
コード例 #19
0
        public void IValidatableObjectWithMultiPropertyValidationFailure()
        {
            // Arrange
            var context = new ControllerContext();
            var validatable = new Mock<IValidatableObject>();
            var metadata = ModelMetadataProviders.Current.GetMetadataForType(() => validatable.Object, validatable.Object.GetType());
            var validator = new ValidatableObjectAdapter(metadata, context);
            validatable.Setup(vo => vo.Validate(It.IsAny<ValidationContext>()))
                .Returns(new[] { new ValidationResult("Error Message", new[] { "Property1", "Property2" }) })
                .Verifiable();

            // Act
            ModelValidationResult[] results = validator.Validate(null).ToArray();

            // Assert
            validatable.Verify();
            Assert.Equal(2, results.Length);
            Assert.Equal("Error Message", results[0].Message);
            Assert.Equal("Property1", results[0].MemberName);
            Assert.Equal("Error Message", results[1].Message);
            Assert.Equal("Property2", results[1].MemberName);
        }