public void Execute_ResolvesView_AndWritesDiagnosticSource() { // Arrange var view = new Mock <IView>(MockBehavior.Strict); view.Setup(v => v.RenderAsync(It.IsAny <ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), "Components/Invoke/Default", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/Default", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData, TempData = _tempDataDictionary, }; var adapter = new TestDiagnosticListener(); var viewComponentContext = GetViewComponentContext(view.Object, viewData, adapter); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.View); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.View); }
private static ViewComponentContext GetViewComponentContext(IView view, ViewDataDictionary viewData, object diagnosticListener = null) { var diagnosticSource = new DiagnosticListener("Microsoft.AspNet"); if (diagnosticListener == null) { diagnosticListener = new TestDiagnosticListener(); } diagnosticSource.SubscribeWithAdapter(diagnosticListener); var serviceProvider = new Mock <IServiceProvider>(); serviceProvider.Setup(s => s.GetService(typeof(DiagnosticSource))).Returns(diagnosticSource); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceProvider.Object; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var viewContext = new ViewContext( actionContext, view, viewData, new TempDataDictionary(new HttpContextAccessor(), new SessionStateTempDataProvider()), TextWriter.Null, new HtmlHelperOptions()); var viewComponentDescriptor = new ViewComponentDescriptor() { Type = typeof(object), }; var viewComponentContext = new ViewComponentContext( viewComponentDescriptor, new object[0], viewContext, TextWriter.Null); return(viewComponentContext); }
public async Task ExecuteResultAsync_ExecutesViewComponent_AndWritesDiagnosticSource() { // Arrange var descriptor = new ViewComponentDescriptor() { FullName = "Full.Name.Text", ShortName = "Text", Type = typeof(TextViewComponent), MethodInfo = typeof(TextViewComponent).GetMethod(nameof(TextViewComponent.Invoke)), }; var adapter = new TestDiagnosticListener(); var actionContext = CreateActionContext(adapter, descriptor); var viewComponentResult = new ViewComponentResult() { Arguments = new { name = "World!" }, ViewComponentName = "Text", TempData = _tempDataDictionary, }; // Act await viewComponentResult.ExecuteResultAsync(actionContext); // Assert var body = ReadBody(actionContext.HttpContext.Response); Assert.Equal("Hello, World!", body); Assert.NotNull(adapter.BeforeViewComponent?.ActionDescriptor); Assert.NotNull(adapter.BeforeViewComponent?.ViewComponentContext); Assert.NotNull(adapter.BeforeViewComponent?.ViewComponent); Assert.NotNull(adapter.AfterViewComponent?.ActionDescriptor); Assert.NotNull(adapter.AfterViewComponent?.ViewComponentContext); Assert.NotNull(adapter.AfterViewComponent?.ViewComponentResult); Assert.NotNull(adapter.AfterViewComponent?.ViewComponent); }
public async Task ExecuteResultAsync_ExecutesViewComponent_AndWritesDiagnosticSource() { // Arrange var descriptor = new ViewComponentDescriptor() { FullName = "Full.Name.Text", ShortName = "Text", Type = typeof(TextViewComponent), MethodInfo = typeof(TextViewComponent).GetMethod(nameof(TextViewComponent.Invoke)), }; var adapter = new TestDiagnosticListener(); var actionContext = CreateActionContext(adapter, descriptor); var viewComponentResult = new ViewComponentResult() { Arguments = new { name = "World!" }, ViewComponentName = "Text", TempData = _tempDataDictionary, }; // Act await viewComponentResult.ExecuteResultAsync(actionContext); // Assert var body = ReadBody(actionContext.HttpContext.Response); Assert.Equal("Hello, World!", body); Assert.NotNull(adapter.BeforeViewComponent?.ActionDescriptor); Assert.NotNull(adapter.BeforeViewComponent?.ViewComponentContext); Assert.NotNull(adapter.BeforeViewComponent?.ViewComponent); Assert.NotNull(adapter.AfterViewComponent?.ActionDescriptor); Assert.NotNull(adapter.AfterViewComponent?.ViewComponentContext); Assert.NotNull(adapter.AfterViewComponent?.ViewComponentResult); Assert.NotNull(adapter.AfterViewComponent?.ViewComponent); }
private static ViewComponentContext GetViewComponentContext( IView view, ViewDataDictionary viewData, object diagnosticListener = null) { var diagnosticSource = new DiagnosticListener("Microsoft.AspNet"); if (diagnosticListener == null) { diagnosticListener = new TestDiagnosticListener(); } diagnosticSource.SubscribeWithAdapter(diagnosticListener); var serviceProvider = new Mock<IServiceProvider>(); serviceProvider.Setup(s => s.GetService(typeof(DiagnosticSource))).Returns(diagnosticSource); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceProvider.Object; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var viewContext = new ViewContext( actionContext, view, viewData, new TempDataDictionary(httpContext, new SessionStateTempDataProvider()), TextWriter.Null, new HtmlHelperOptions()); var viewComponentDescriptor = new ViewComponentDescriptor() { ShortName = "Invoke", Type = typeof(object), MethodInfo = typeof(object).GetTypeInfo().DeclaredMethods.First() }; var viewComponentContext = new ViewComponentContext( viewComponentDescriptor, new Dictionary<string, object>(), new HtmlTestEncoder(), viewContext, TextWriter.Null); return viewComponentContext; }
public void Execute_ResolvesView_AndWritesDiagnosticSource() { // Arrange var view = new Mock<IView>(MockBehavior.Strict); view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>())) .Returns(Task.FromResult(result: true)) .Verifiable(); var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.FindView(It.IsAny<ActionContext>(), "Components/Invoke/Default", /*isMainPage*/ false)) .Returns(ViewEngineResult.Found("Components/Invoke/Default", view.Object)) .Verifiable(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var result = new ViewViewComponentResult { ViewEngine = viewEngine.Object, ViewData = viewData, TempData = _tempDataDictionary, }; var adapter = new TestDiagnosticListener(); var viewComponentContext = GetViewComponentContext(view.Object, viewData, adapter); // Act result.Execute(viewComponentContext); // Assert viewEngine.Verify(); view.Verify(); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentBeforeViewExecute?.View); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ActionDescriptor); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.ViewComponentContext); Assert.NotNull(adapter.ViewComponentAfterViewExecute?.View); }
private static ViewComponentContext GetViewComponentContext(IView view, ViewDataDictionary viewData, object diagnosticListener = null) { var diagnosticSource = new DiagnosticListener("Microsoft.AspNet"); if (diagnosticListener == null) { diagnosticListener = new TestDiagnosticListener(); } diagnosticSource.SubscribeWithAdapter(diagnosticListener); var serviceProvider = new Mock<IServiceProvider>(); serviceProvider.Setup(s => s.GetService(typeof(DiagnosticSource))).Returns(diagnosticSource); var httpContext = new DefaultHttpContext(); httpContext.RequestServices = serviceProvider.Object; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); var viewContext = new ViewContext( actionContext, view, viewData, new TempDataDictionary(new HttpContextAccessor(), new SessionStateTempDataProvider()), TextWriter.Null, new HtmlHelperOptions()); var viewComponentDescriptor = new ViewComponentDescriptor() { Type = typeof(object), }; var viewComponentContext = new ViewComponentContext( viewComponentDescriptor, new object[0], viewContext, TextWriter.Null); return viewComponentContext; }