protected ModelValidationContext GetMockedValidationContextWithContainer(ValidatableSampleModel model)
        {
            var container     = new ValidatableSampleModelContainer();
            var options       = _provider.GetRequiredService <IOptions <MvcOptions> >();
            var actionContext = ValidatorUtils.CreateActionContext();
            var provider      = new DefaultModelMetadataProvider(_compositeMetadataDetailsProvider, options);
            var metadata      = provider.GetMetadataForType(model.GetType());

            var context = new ModelValidationContext(actionContext, metadata, provider, container, model);

            return(context);
        }
        protected ModelValidationContext GetModelValidationContextWithoutContainer(ValidatableSampleModel model)
        {
            var options       = _provider.GetRequiredService <IOptions <MvcOptions> >();
            var actionContext = ValidatorUtils.CreateActionContext();
            var provider      = new DefaultModelMetadataProvider(_compositeMetadataDetailsProvider, options);
            var metadata      = provider.GetMetadataForProperty(
                typeof(ValidatableSampleModelContainer),
                nameof(ValidatableSampleModelContainer.SampleModel));

            var context = new ModelValidationContext(actionContext, metadata, provider, null, model);

            return(context);
        }
예제 #3
0
        public void PassExpectedNames()
        {
            var model = new ValidatableSampleModel();

            var context = GetMockedValidationContextWithContainer(model);

            var results = _adapter.Validate(context);

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

            Assert.Equal(nameof(ValidatableSampleModel), model.DisplayName);
            Assert.Null(model.MemberName);
            Assert.Equal(model, model.ObjectInstance);
        }
예제 #4
0
        public void ReturnExpectedResults()
        {
            var model                 = new ValidatableSampleModel();
            var validationResult      = new ValidationResult("Error message");
            var modelValidationResult = new ModelValidationResult(null, "Error message");
            var expectedResults       = new List <ModelValidationResult>
            {
                modelValidationResult,
            };

            model.ValidationResults.Add(validationResult);

            var context = GetModelValidationContextWithoutContainer(model);

            var results = _adapter.Validate(context);

            Assert.NotNull(results);
            Assert.Equal(expectedResults, results, TestModelValidationResultComparer.Instance);
        }