public void Activate_ContextualizesServices_AndSetsProperties_OnPage() { // Arrange var activator = CreateActivator(); var instance = new TestRazorPage(); var viewData = new ViewDataDictionary <MyModel>(MetadataProvider, new ModelStateDictionary()); var viewContext = CreateViewContext(); var urlHelper = UrlHelperFactory.GetUrlHelper(viewContext); // Act activator.Activate(instance, viewContext); // Assert Assert.Same(DiagnosticListener, instance.DiagnosticSource); Assert.Same(HtmlEncoder, instance.HtmlEncoder); Assert.Same(JsonHelper, instance.Json); Assert.Same(urlHelper, instance.Url); Assert.Same(viewContext.ViewData, instance.ViewData); // Has no [RazorInject] so it shouldn't get injected Assert.Null(instance.MyService2); // We're not testing the IViewContextualizable implementation here because it's a mock. Assert.NotNull(instance.Html); Assert.IsAssignableFrom <IHtmlHelper <object> >(instance.Html); var service = instance.MyService; Assert.NotNull(service); Assert.Same(viewContext, service.ViewContext); }
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); }
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); }
public void Activate_ActivatesAndContextualizesPropertiesOnViews() { // Arrange var modelMetadataProvider = new EmptyModelMetadataProvider(); var modelExpressionProvider = new ModelExpressionProvider(modelMetadataProvider, new ExpressionTextCache()); var urlHelperFactory = new UrlHelperFactory(); var jsonHelper = new JsonHelper( new JsonOutputFormatter(new JsonSerializerSettings(), ArrayPool <char> .Shared), ArrayPool <char> .Shared); var htmlEncoder = new HtmlTestEncoder(); var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore"); var activator = new RazorPageActivator( new EmptyModelMetadataProvider(), urlHelperFactory, jsonHelper, diagnosticSource, htmlEncoder, modelExpressionProvider); var instance = new TestRazorPage(); var myService = new MyService(); var helper = Mock.Of <IHtmlHelper <object> >(); var serviceProvider = new ServiceCollection() .AddSingleton(myService) .AddSingleton(helper) .AddSingleton(new ExpressionTextCache()) .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(), new ModelStateDictionary()), Mock.Of <ITempDataDictionary>(), TextWriter.Null, new HtmlHelperOptions()); var urlHelper = urlHelperFactory.GetUrlHelper(viewContext); // 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.Same(htmlEncoder, instance.HtmlEncoder); Assert.Same(jsonHelper, instance.Json); Assert.Same(urlHelper, instance.Url); Assert.Null(instance.MyService2); }
public void GetOrAddCacheEntry_CachesPages() { // Arrange var activator = CreateActivator(); var page = new TestRazorPage(); // Act var result1 = activator.GetOrAddCacheEntry(page); var result2 = activator.GetOrAddCacheEntry(page); // Assert Assert.Same(result1, result2); }
public void Activate_InstantiatesNewViewDataDictionaryType_IfTheTypeDoesNotMatch() { // 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 TestRazorPage(); var myService = new MyService(); var helper = Mock.Of <IHtmlHelper <object> >(); var serviceProvider = new ServiceCollection() .AddSingleton(myService) .AddSingleton(helper) .AddSingleton(new ExpressionTextCache()) .BuildServiceProvider(); var httpContext = new DefaultHttpContext { RequestServices = serviceProvider }; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var viewData = new ViewDataDictionary <object>(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { 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); }
public void Activate_InstantiatesNewViewDataDictionaryType_IfTheTypeDoesNotMatch() { // Arrange var activator = CreateActivator(); var viewData = new ViewDataDictionary <object>(MetadataProvider, new ModelStateDictionary()) { { "key", "value" }, }; var viewContext = CreateViewContext(viewData); var urlHelper = UrlHelperFactory.GetUrlHelper(viewContext); var instance = new TestRazorPage(); // Act activator.Activate(instance, viewContext); // Assert Assert.Same(DiagnosticListener, instance.DiagnosticSource); Assert.Same(HtmlEncoder, instance.HtmlEncoder); Assert.Same(JsonHelper, instance.Json); Assert.Same(urlHelper, instance.Url); Assert.Same(viewContext.ViewData, instance.ViewData); // The original ViewDataDictionary was replaced. Assert.NotSame(viewData, viewContext.ViewData); Assert.NotSame(viewData, instance.ViewData); // But this value is copied Assert.Equal("value", viewData["key"]); // Has no [RazorInject] so it shouldn't get injected Assert.Null(instance.MyService2); // We're not testing the IViewContextualizable implementation here because it's a mock. Assert.NotNull(instance.Html); Assert.IsAssignableFrom <IHtmlHelper <object> >(instance.Html); var service = instance.MyService; Assert.NotNull(service); Assert.Same(viewContext, service.ViewContext); }
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.AspNetCore"); var serviceProvider = new ServiceCollection() .AddSingleton(myService) .AddSingleton(helper) .AddSingleton <HtmlEncoder>(htmlEncoder) .AddSingleton(new ExpressionTextCache()) .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); }
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); }
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 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 <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); }