コード例 #1
0
        // GetCurrentValues uses only the IModelMetadataProvider passed to the DefaultHtmlGenerator constructor.
        private static IHtmlGenerator GetGenerator(IModelMetadataProvider metadataProvider)
        {
            var mvcViewOptionsAccessor = new Mock <IOptions <MvcViewOptions> >();

            mvcViewOptionsAccessor.SetupGet(accessor => accessor.Value).Returns(new MvcViewOptions()
            {
                AllowRenderingMaxLengthAttribute = true
            });

            var htmlEncoder = Mock.Of <HtmlEncoder>();
            var antiforgery = new Mock <IAntiforgery>();

            antiforgery
            .Setup(mock => mock.GetAndStoreTokens(It.IsAny <DefaultHttpContext>()))
            .Returns(() =>
            {
                return(new AntiforgeryTokenSet("requestToken", "cookieToken", "formFieldName", "headerName"));
            });

            var attributeProvider = new DefaultValidationHtmlAttributeProvider(
                mvcViewOptionsAccessor.Object,
                metadataProvider,
                new ClientValidatorCache());

            return(new DefaultHtmlGenerator(
                       antiforgery.Object,
                       mvcViewOptionsAccessor.Object,
                       metadataProvider,
                       new UrlHelperFactory(),
                       htmlEncoder,
                       attributeProvider));
        }
コード例 #2
0
        public static IHtmlGenerator GetHtmlGenerator(IModelMetadataProvider provider, IUrlHelperFactory urlHelperFactory, MvcViewOptions options)
        {
            var optionsAccessor = new Mock <IOptions <MvcViewOptions> >();

            optionsAccessor
            .SetupGet(o => o.Value)
            .Returns(options);

            var attributeProvider = new DefaultValidationHtmlAttributeProvider(
                optionsAccessor.Object,
                provider,
                new ClientValidatorCache());

            var htmlGenerator = new DefaultHtmlGenerator(
                Mock.Of <IAntiforgery>(),
                optionsAccessor.Object,
                provider,
                urlHelperFactory,
                new HtmlTestEncoder(),
                attributeProvider);

            return(htmlGenerator);
        }