Exemple #1
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);
        }
        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 <IReadOnlyList <IInputFormatter> >(),
                       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 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,
                validator,
                bodyValidationPredicatesProvider.Object);

            return(binder);
        }