コード例 #1
0
        private static BodyModelBinder GetBodyBinder(
            IInputFormatter inputFormatter, IBodyModelValidator validator, IOptions <MvcOptions> mvcOptions)
        {
            var actionContext          = CreateActionContext(new DefaultHttpContext());
            var inputFormatterSelector = new Mock <IInputFormatterSelector>();

            inputFormatterSelector.Setup(o => o.SelectFormatter(It.IsAny <InputFormatterContext>())).Returns(inputFormatter);

            if (validator == null)
            {
                var mockValidator = new Mock <IBodyModelValidator>();
                mockValidator.Setup(o => o.Validate(It.IsAny <ModelValidationContext>(), It.IsAny <string>()))
                .Returns(true)
                .Verifiable();
                validator = mockValidator.Object;
            }

            if (mvcOptions == null)
            {
                var options = new Mock <MvcOptions>();
                options.CallBase = true;
                var mockMvcOptions = new Mock <IOptions <MvcOptions> >();
                mockMvcOptions.SetupGet(o => o.Options).Returns(options.Object);
                mvcOptions = mockMvcOptions.Object;
            }

            var binder = new BodyModelBinder(actionContext,
                                             inputFormatterSelector.Object,
                                             validator,
                                             mvcOptions);

            return(binder);
        }
コード例 #2
0
        private static BodyModelBinder GetBodyBinder(
            IInputFormatter inputFormatter, IBodyModelValidator validator)
        {
            var actionContext          = CreateActionContext(new DefaultHttpContext());
            var inputFormatterSelector = new Mock <IInputFormatterSelector>();

            inputFormatterSelector.Setup(o => o.SelectFormatter(It.IsAny <InputFormatterContext>())).Returns(inputFormatter);

            if (validator == null)
            {
                var mockValidator = new Mock <IBodyModelValidator>();
                mockValidator.Setup(o => o.Validate(It.IsAny <ModelValidationContext>(), It.IsAny <string>()))
                .Returns(true)
                .Verifiable();
                validator = mockValidator.Object;
            }

            var bodyValidationPredicatesProvidwer = new Mock <IValidationExcludeFiltersProvider>();

            bodyValidationPredicatesProvidwer.SetupGet(o => o.ExcludeFilters)
            .Returns(new List <IExcludeTypeValidationFilter>());

            var binder = new BodyModelBinder(actionContext,
                                             inputFormatterSelector.Object,
                                             validator,
                                             bodyValidationPredicatesProvidwer.Object);

            return(binder);
        }
コード例 #3
0
        private static BodyModelBinder GetBodyBinder(HttpContext httpContext, IInputFormatter inputFormatter)
        {
            var actionContext          = CreateActionContext(httpContext);
            var inputFormatterSelector = new Mock <IInputFormatterSelector>();

            inputFormatterSelector
            .Setup(o => o.SelectFormatter(
                       It.IsAny <IReadOnlyList <IInputFormatter> >(),
                       It.IsAny <InputFormatterContext>()))
            .Returns(inputFormatter);

            var bodyValidationPredicatesProvider = new Mock <IValidationExcludeFiltersProvider>();

            bodyValidationPredicatesProvider.SetupGet(o => o.ExcludeFilters)
            .Returns(new List <IExcludeTypeValidationFilter>());

            var bindingContext = new ActionBindingContext()
            {
                InputFormatters = new List <IInputFormatter>(),
            };

            var bindingContextAccessor = new MockScopedInstance <ActionBindingContext>()
            {
                Value = bindingContext,
            };

            var binder = new BodyModelBinder(
                actionContext,
                bindingContextAccessor,
                inputFormatterSelector.Object,
                bodyValidationPredicatesProvider.Object);

            return(binder);
        }