protected ModelValidatorProviderContext GetMockedContext() { var options = _provider.GetRequiredService <IOptions <MvcOptions> >(); var provider = new DefaultModelMetadataProvider(_compositeMetadataDetailsProvider, options); var metadata = provider.GetMetadataForType(typeof(ValidatableObject)); var context = new ModelValidatorProviderContext(metadata, GetValidatorItems(metadata)); return(context); }
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 ModelExpression MakeExpression <TModel>(TModel model) { var modelExpressionProvider = ServiceProvider.GetService <ModelExpressionProvider>(); var compositeMetadataDetailsProvider = ServiceProvider.GetService <ICompositeMetadataDetailsProvider>(); var metadataProvider = new DefaultModelMetadataProvider(compositeMetadataDetailsProvider); var viewDataDictionary = new ViewDataDictionary <TModel>(metadataProvider, new ModelStateDictionary()) { Model = model }; var modelExplorer = new ModelExplorer( metadataProvider, metadataProvider.GetMetadataForType(model.GetType()), model); return(new ModelExpression("Model", modelExplorer)); }
protected ModelExpression MakeExpression <TModel>(TModel viewModel, string propertyName, object propertyContent) { var containerType = viewModel.GetType(); var m3 = ServiceProvider.GetService <ModelExpressionProvider>(); var compositeMetadataDetailsProvider = ServiceProvider.GetService <ICompositeMetadataDetailsProvider>(); var metadataProvider = new DefaultModelMetadataProvider(compositeMetadataDetailsProvider); var containerMetadata = metadataProvider.GetMetadataForType(containerType); var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, viewModel); var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, propertyName); var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, propertyContent); return(new ModelExpression(propertyName, modelExplorer)); }
public void WhenModelTypeIsOfTypeUnknown_ItShouldReturnNull() { // Arrange var modelBinderProvider = new IdentifierModelBinderProvider <int>(); var modelBinderProviderContext = new Mock <ModelBinderProviderContext>(); // modelBinderProviderContext.Metadata.ModelType var compositeMetadataDetailsProvider = new Mock <ICompositeMetadataDetailsProvider>(); var data = new DefaultModelMetadataProvider(compositeMetadataDetailsProvider.Object); var modelMetadata = data.GetMetadataForType(typeof(DateTime)); modelBinderProviderContext.Setup(x => x.Metadata) .Returns(modelMetadata); // Act var modelBinder = modelBinderProvider.GetBinder(modelBinderProviderContext.Object); // Assert Assert.Null(modelBinder); }
public void WhenModelValueForModelNameIsNull_ItShouldReturnCompletedTask() { // Arrange var modelName = "id"; var valueProviderResult = new ValueProviderResult(new StringValues("")); var compositeMetadataDetailsProvider = new Mock <ICompositeMetadataDetailsProvider>(); var data = new DefaultModelMetadataProvider(compositeMetadataDetailsProvider.Object); var modelMetadata = data.GetMetadataForType(typeof(Identifier)); var valueProvider = new Mock <IValueProvider>(); valueProvider.Setup(x => x.GetValue(modelName)) .Returns(valueProviderResult); var bindingContext = new DefaultModelBindingContext { ModelName = modelName, ModelState = new ModelStateDictionary(), ValueProvider = valueProvider.Object, ModelMetadata = modelMetadata }; var modelBinder = new IdentifierModelBinder <int>(); // Act var task = modelBinder.BindModelAsync(bindingContext); // Assert Assert.Same(Task.CompletedTask, task); Assert.False(bindingContext.Result.IsModelSet); Assert.Null(bindingContext.Result.Model); Assert.Equal("Failed", bindingContext.Result.ToString()); }