public void ValidationProviders_ReturnsInstantiatedListOfValueProviders()
        {
            // Arrange
            var service = Mock.Of<ITestService>();
            var validationProvider = Mock.Of<IModelValidatorProvider>();
            var type = typeof(TestModelValidationProvider);
            var serviceProvider = new Mock<IServiceProvider>();
            serviceProvider.Setup(p => p.GetService(typeof(ITestService)))
                           .Returns(service);
            var typeActivatorCache = new DefaultTypeActivatorCache();
            var options = new MvcOptions();
            options.ModelValidatorProviders.Add(type);
            options.ModelValidatorProviders.Add(validationProvider);
            var accessor = new Mock<IOptions<MvcOptions>>();
            accessor.SetupGet(a => a.Options)
                    .Returns(options);
            var provider = new DefaultModelValidatorProviderProvider(accessor.Object,
                                                                     typeActivatorCache,
                                                                     serviceProvider.Object);

            // Act
            var result = provider.ModelValidatorProviders;

            // Assert
            Assert.Equal(2, result.Count);
            var testModelValidationProvider = Assert.IsType<TestModelValidationProvider>(result[0]);
            Assert.Same(service, testModelValidationProvider.Service);
            Assert.Same(validationProvider, result[1]);
        }
示例#2
0
        private static ControllerActionInvoker GetControllerActionInvoker(
            string input, Type parameterType, IInputFormatter selectedFormatter, string contentType)
        {
            var mvcOptions = new MvcOptions();
            var setup      = new MvcOptionsSetup();

            setup.Configure(mvcOptions);
            var accessor = new Mock <IOptions <MvcOptions> >();

            accessor.SetupGet(a => a.Options)
            .Returns(mvcOptions);
            var validatorProvider = new DefaultModelValidatorProviderProvider(
                accessor.Object, Mock.Of <ITypeActivator>(), Mock.Of <IServiceProvider>());

            Func <object, int> method = x => 1;
            var actionDescriptor      = new ControllerActionDescriptor
            {
                MethodInfo = method.Method,
                Parameters = new List <ParameterDescriptor>
                {
                    new ParameterDescriptor
                    {
                        BinderMetadata = new FromBodyAttribute(),
                        Name           = "foo",
                        ParameterType  = parameterType,
                    }
                }
            };

            var metadataProvider = new EmptyModelMetadataProvider();
            var actionContext    = GetActionContext(
                Encodings.UTF8EncodingWithoutBOM.GetBytes(input), actionDescriptor, contentType);

            var inputFormatterSelector = new Mock <IInputFormatterSelector>();

            inputFormatterSelector.Setup(a => a.SelectFormatter(It.IsAny <InputFormatterContext>()))
            .Returns(selectedFormatter);
            var bindingContext = new ActionBindingContext(actionContext,
                                                          metadataProvider,
                                                          Mock.Of <IModelBinder>(),
                                                          Mock.Of <IValueProvider>(),
                                                          inputFormatterSelector.Object,
                                                          new CompositeModelValidatorProvider(validatorProvider));

            var actionBindingContextProvider = new Mock <IActionBindingContextProvider>();

            actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny <ActionContext>()))
            .Returns(Task.FromResult(bindingContext));

            var inputFormattersProvider = new Mock <IInputFormattersProvider>();

            inputFormattersProvider.SetupGet(o => o.InputFormatters)
            .Returns(new List <IInputFormatter>());
            return(new ControllerActionInvoker(actionContext,
                                               Mock.Of <INestedProviderManager <FilterProviderContext> >(),
                                               Mock.Of <IControllerFactory>(),
                                               actionDescriptor,
                                               inputFormattersProvider.Object,
                                               Mock.Of <IControllerActionArgumentBinder>()));
        }