protected ModelValidationContext GetMockedValidationContextForModel(object model, object container = null)
        {
            var actionContext = ValidatorUtils.CreateActionContext();
            var options       = _provider.GetRequiredService <IOptions <MvcOptions> >();
            var provider      = new DefaultModelMetadataProvider(_compositeMetadataDetailsProvider, options);
            var metadata      = provider.GetMetadataForProperty(typeof(RequiredModel).GetProperty("Summary"), typeof(RequiredModel));

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

            return(context);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void NotPassRequired()
        {
            var model = new RequiredModel
            {
                Summary = string.Empty,
            };

            var actionContext = ValidatorUtils.CreateActionContext();

            _validator.Validate(actionContext, null, string.Empty, model);

            Assert.False(actionContext.ModelState.IsValid);
            Assert.NotEmpty(actionContext.ModelState);
        }
コード例 #5
0
        public void PassRequired()
        {
            var model = new RequiredModel
            {
                Summary = Guid.NewGuid().ToString(),
            };

            var actionContext = ValidatorUtils.CreateActionContext();

            _validator.Validate(actionContext, null, string.Empty, model);

            Assert.True(actionContext.ModelState.IsValid);
            Assert.Empty(actionContext.ModelState);
        }