public void Configure(MvcOptions options)
        {
            var fallbackBinderProvider = options.ModelBinderProviders.First(x => x is ComplexTypeModelBinderProvider);
            var binderProvider         = new GenericSearchModelBinderProvider(fallbackBinderProvider);

            options.ModelBinderProviders.Insert(0, binderProvider);
        }
        public void Null_Configuration_Succeeds()
        {
            var configurationProvider    = new ListConfigurationProvider(new List <IListDefinitionSource>(), null, optionsMock.Object);
            var requestActivator         = new Mock <IModelActivator>();
            var requestPropertyActivator = new Mock <IModelPropertyActivator>();

            var services = new ServiceCollection()
                           .AddSingleton <IListConfigurationProvider>(configurationProvider)
                           .AddSingleton(requestActivator.Object)
                           .AddSingleton(requestPropertyActivator.Object)
                           .BuildServiceProvider();

            var context        = new TestModelBinderContext(typeof(Request), services);
            var fallbackBinder = new Mock <IModelBinderProvider>();

            var provider = new GenericSearchModelBinderProvider(fallbackBinder.Object);

            var modelBinder = provider.GetBinder(context);

            modelBinder.Should().BeNull();
        }
        public void Valid_Configuration_Succeeds()
        {
            var configuration = new ListConfiguration(typeof(Request), typeof(Item), typeof(Result));

            var configurationProvider = new Mock <IListConfigurationProvider>();

            configurationProvider.Setup(x => x.GetConfiguration(It.IsAny <Type>())).Returns(configuration);
            var requestActivator         = new Mock <IModelActivator>();
            var requestPropertyActivator = new Mock <IModelPropertyActivator>();
            var modelCache = new Mock <IModelCache>();

            var httpContext = new Mock <HttpContext>();
            var accessor    = new TestHttpContextAccessor(httpContext.Object);

            var services = new ServiceCollection()
                           .AddSingleton(configurationProvider.Object)
                           .AddSingleton(requestActivator.Object)
                           .AddSingleton(requestPropertyActivator.Object)
                           .AddSingleton(modelCache.Object)
                           .AddSingleton <IHttpContextAccessor>(accessor)
                           .BuildServiceProvider();

            httpContext.SetupGet(x => x.RequestServices).Returns(services);

            var context = new TestModelBinderContext(typeof(Request), services);

            var fallbackModelBinder         = new Mock <IModelBinder>();
            var fallbackModelBinderProvider = new Mock <IModelBinderProvider>();

            fallbackModelBinderProvider.Setup(x => x.GetBinder(It.IsAny <ModelBinderProviderContext>())).Returns(fallbackModelBinder.Object);

            var provider = new GenericSearchModelBinderProvider(fallbackModelBinderProvider.Object);

            var modelBinder = provider.GetBinder(context);

            modelBinder.Should().BeOfType <GenericSearchModelBinder>();
        }