Ejemplo n.º 1
0
        public void Activate_ActivatesAndContextualizesPropertiesOnViews()
        {
            // Arrange
            var activator = new RazorPageActivator(Mock.Of <ITypeActivator>());
            var instance  = new TestRazorPage();

            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(MyService)))
            .Returns(myService);
            serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper <object>)))
            .Returns(helper);
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);
            var routeContext  = new RouteContext(httpContext.Object);
            var actionContext = new ActionContext(routeContext, new ActionDescriptor());
            var viewContext   = new ViewContext(actionContext,
                                                Mock.Of <IView>(),
                                                new ViewDataDictionary(new EmptyModelMetadataProvider()),
                                                TextWriter.Null);

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

            // Assert
            Assert.Same(helper, instance.Html);
            Assert.Same(myService, instance.MyService);
            Assert.Same(viewContext, myService.ViewContext);
            Assert.Null(instance.MyService2);
        }
Ejemplo n.º 2
0
        public void Activate_ThrowsIfTheViewDoesNotDeriveFromRazorViewOfT()
        {
            // Arrange
            var activator = new RazorPageActivator(Mock.Of <ITypeActivator>());
            var instance  = new DoesNotDeriveFromRazorPageOfT();

            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var serviceProvider = new Mock <IServiceProvider>();
            var httpContext     = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);
            var routeContext  = new RouteContext(httpContext.Object);
            var actionContext = new ActionContext(routeContext, new ActionDescriptor());
            var viewContext   = new ViewContext(actionContext,
                                                Mock.Of <IView>(),
                                                new ViewDataDictionary(new EmptyModelMetadataProvider()),
                                                TextWriter.Null);

            // Act and Assert
            var ex      = Assert.Throws <InvalidOperationException>(() => activator.Activate(instance, viewContext));
            var message = string.Format(CultureInfo.InvariantCulture,
                                        "View of type '{0}' cannot be activated by '{1}'.",
                                        instance.GetType().FullName,
                                        typeof(RazorPageActivator).FullName);

            Assert.Equal(message, ex.Message);
        }
Ejemplo n.º 3
0
        public void Activate_UsesPassedInViewDataDictionaryInstance_IfPassedInTypeMatches()
        {
            // Arrange
            var typeActivator   = new TypeActivator();
            var activator       = new RazorPageActivator(typeActivator);
            var instance        = new TestRazorPage();
            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(MyService)))
            .Returns(myService);
            serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper <object>)))
            .Returns(helper);
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);
            var routeContext  = new RouteContext(httpContext.Object);
            var actionContext = new ActionContext(routeContext, new ActionDescriptor());
            var viewData      = new ViewDataDictionary <MyModel>(new EmptyModelMetadataProvider())
            {
                Model = new MyModel()
            };
            var viewContext = new ViewContext(actionContext,
                                              Mock.Of <IView>(),
                                              viewData,
                                              TextWriter.Null);

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

            // Assert
            Assert.Same(viewData, viewContext.ViewData);
        }
Ejemplo n.º 4
0
        public void Activate_DeterminesModelTypeFromProperty()
        {
            // Arrange
            var typeActivator   = new TypeActivator();
            var activator       = new RazorPageActivator(typeActivator);
            var instance        = new DoesNotDeriveFromRazorPageOfTButHasModelProperty();
            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(MyService)))
            .Returns(myService);
            serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper <object>)))
            .Returns(helper);
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);
            var routeContext  = new RouteContext(httpContext.Object);
            var actionContext = new ActionContext(routeContext, new ActionDescriptor());
            var viewData      = new ViewDataDictionary(new EmptyModelMetadataProvider());
            var viewContext   = new ViewContext(actionContext,
                                                Mock.Of <IView>(),
                                                viewData,
                                                TextWriter.Null);

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

            // Assert
            Assert.IsType <ViewDataDictionary <string> >(viewContext.ViewData);
        }
Ejemplo n.º 5
0
        public void Activate_ThrowsIfTheViewDoesNotDeriveFromRazorViewOfT()
        {
            // Arrange
            var activator = new RazorPageActivator(new EmptyModelMetadataProvider());
            var instance  = new DoesNotDeriveFromRazorPageOfT();

            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var serviceProvider = new Mock <IServiceProvider>();
            var httpContext     = new DefaultHttpContext
            {
                RequestServices = new ServiceCollection().BuildServiceProvider()
            };

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

            // Act and Assert
            var ex      = Assert.Throws <InvalidOperationException>(() => activator.Activate(instance, viewContext));
            var message = $"View of type '{instance.GetType()}' cannot be activated by '{typeof(RazorPageActivator)}'.";

            Assert.Equal(message, ex.Message);
        }
Ejemplo n.º 6
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 <DiagnosticSource>(new DiagnosticListener("Microsoft.AspNet.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);
        }
Ejemplo n.º 7
0
        public void Activate_InstantiatesNewViewDataDictionaryType_IfTheTypeDoesNotMatch()
        {
            // Arrange
            var activator = new RazorPageActivator(new EmptyModelMetadataProvider());
            var instance  = new TestRazorPage();

            var myService       = new MyService();
            var helper          = Mock.Of <IHtmlHelper <object> >();
            var htmlEncoder     = new CommonTestEncoder();
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(MyService)))
            .Returns(myService);
            serviceProvider.Setup(p => p.GetService(typeof(IHtmlHelper <object>)))
            .Returns(helper);
            serviceProvider.Setup(p => p.GetService(typeof(IHtmlEncoder)))
            .Returns(htmlEncoder);
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(serviceProvider.Object);

            var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
            var viewData      = new ViewDataDictionary <object>(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.IsType <ViewDataDictionary <MyModel> >(viewContext.ViewData);
        }
Ejemplo n.º 8
0
        public void Activate_ActivatesAndContextualizesPropertiesOnViews()
        {
            // 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 diagnosticSource = new DiagnosticListener("Microsoft.AspNet");
            var serviceProvider  = new ServiceCollection()
                                   .AddSingleton(myService)
                                   .AddSingleton(helper)
                                   .AddSingleton <HtmlEncoder>(htmlEncoder)
                                   .AddSingleton <DiagnosticSource>(diagnosticSource)
                                   .BuildServiceProvider();
            var httpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider
            };

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

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

            // Assert
            Assert.Same(helper, instance.Html);
            Assert.Same(myService, instance.MyService);
            Assert.Same(viewContext, myService.ViewContext);
            Assert.Same(diagnosticSource, instance.DiagnosticSource);
            Assert.Null(instance.MyService2);
        }
Ejemplo n.º 9
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.Aspnet.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);
        }