Пример #1
0
 public RazorPageActivator(
     IModelMetadataProvider metadataProvider,
     IUrlHelperFactory urlHelperFactory,
     IJsonHelper jsonHelper,
     DiagnosticSource diagnosticSource,
     HtmlEncoder htmlEncoder,
     IModelExpressionProvider modelExpressionProvider)
 {
     _RazorPageActivator = new MvcRazor.RazorPageActivator(metadataProvider, urlHelperFactory, jsonHelper, diagnosticSource, htmlEncoder, modelExpressionProvider);
 }
Пример #2
0
        public void Activate_UsesPassedInViewDataDictionaryInstance_IfPassedInTypeMatches()
        {
            // Arrange
            var activator       = new RazorPageActivator(new EmptyModelMetadataProvider());
            var instance        = new TestRazorPage();
            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var htmlEncoder     = new HtmlTestEncoder();
            var serviceProvider = new ServiceCollection()
                                  .AddSingleton(myService)
                                  .AddSingleton(helper)
                                  .AddSingleton <HtmlEncoder>(htmlEncoder)
                                  .AddSingleton <DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore.Mvc"))
                                  .BuildServiceProvider();
            var httpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider
            };

            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewData      = new ViewDataDictionary <MyModel>(new EmptyModelMetadataProvider())
            {
                Model = new MyModel()
            };
            var viewContext = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                viewData,
                Mock.Of <ITempDataDictionary>(),
                TextWriter.Null,
                new HtmlHelperOptions());

            // Act
            activator.Activate(instance, viewContext);

            // Assert
            Assert.Same(viewData, viewContext.ViewData);
        }
Пример #3
0
        public void Activate_Throws_WhenViewDataPropertyHasIncorrectType()
        {
            // Arrange
            var modelMetadataProvider   = new EmptyModelMetadataProvider();
            var modelExpressionProvider = new ModelExpressionProvider(modelMetadataProvider, new ExpressionTextCache());
            var activator = new RazorPageActivator(
                modelMetadataProvider,
                new UrlHelperFactory(),
                new JsonHelper(
                    new JsonOutputFormatter(new JsonSerializerSettings(), ArrayPool <char> .Shared),
                    ArrayPool <char> .Shared),
                new DiagnosticListener("Microsoft.AspNetCore.Mvc"),
                new HtmlTestEncoder(),
                modelExpressionProvider);

            var instance = new HasIncorrectViewDataPropertyType();

            var collection = new ServiceCollection();

            collection.AddSingleton(new ExpressionTextCache());
            var httpContext = new DefaultHttpContext
            {
                RequestServices = collection.BuildServiceProvider(),
            };

            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewContext   = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()),
                Mock.Of <ITempDataDictionary>(),
                TextWriter.Null,
                new HtmlHelperOptions());

            // Act & Assert
            Assert.Throws <InvalidCastException>(() => activator.Activate(instance, viewContext));
        }
Пример #4
0
        public void Activate_DeterminesModelTypeFromProperty()
        {
            // Arrange
            var activator       = new RazorPageActivator(new EmptyModelMetadataProvider());
            var instance        = new DoesNotDeriveFromRazorPageOfTButHasModelProperty();
            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var htmlEncoder     = new HtmlTestEncoder();
            var serviceProvider = new ServiceCollection()
                                  .AddSingleton(myService)
                                  .AddSingleton(helper)
                                  .AddSingleton <HtmlEncoder>(htmlEncoder)
                                  .AddSingleton(new ExpressionTextCache())
                                  .AddSingleton <DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore.Mvc"))
                                  .BuildServiceProvider();
            var httpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider
            };

            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewData      = new ViewDataDictionary <object>(new EmptyModelMetadataProvider());
            var viewContext   = new ViewContext(
                actionContext,
                Mock.Of <IView>(),
                viewData,
                Mock.Of <ITempDataDictionary>(),
                TextWriter.Null,
                new HtmlHelperOptions());

            // Act
            activator.Activate(instance, viewContext);

            // Assert
            Assert.IsType <ViewDataDictionary <string> >(viewContext.ViewData);
        }