public async Task ProcessAsync_WithoutSpecifyingRenderMode_ThrowsError() { // Arrange var tagHelper = new ComponentTagHelper { ViewContext = GetViewContext(), }; var context = GetTagHelperContext(); var output = GetTagHelperOutput(); // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output)); Assert.Equal("A value for the 'render-mode' attribute must be supplied to the 'component' tag helper.", ex.Message); }
public async Task ProcessAsync_RendersComponent() { // Arrange var tagHelper = new ComponentTagHelper { ViewContext = GetViewContext(), RenderMode = RenderMode.Static, }; var context = GetTagHelperContext(); var output = GetTagHelperOutput(); // Act await tagHelper.ProcessAsync(context, output); // Assert var content = HtmlContentUtilities.HtmlContentToString(output.Content); Assert.Equal("Hello world", content); Assert.Null(output.TagName); }
public static async Task <IHtmlContent> RenderComponentAsync( Type componentType, Action <IServiceCollection> configureServices, IDictionary <string, object> parameters = null, RenderMode renderMode = RenderMode.Static) { var diContainer = new ServiceCollection(); diContainer.AddLogging(); diContainer.AddRazorPages(); configureServices(diContainer); diContainer.TryAddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5000/") }); using var serviceProvider = diContainer.BuildServiceProvider(); using var scope = serviceProvider.CreateScope(); var featureCollection = new FeatureCollection(); featureCollection.Set <IHttpRequestFeature>(new HttpRequestFeature { Protocol = "HTTP/2", Scheme = "http", Method = "GET", PathBase = "", Path = "/", QueryString = "", RawTarget = "/", Headers = { { "Host", "localhost:5000" } } }); featureCollection.Set <IHttpResponseFeature>(new HttpResponseFeature()); var httpContextFactory = new DefaultHttpContextFactory(scope.ServiceProvider); var httpContext = httpContextFactory.Create(featureCollection); var attributes = new TagHelperAttributeList(); var tagHelperContext = new TagHelperContext(attributes, new Dictionary <object, object>(), Guid.NewGuid().ToString("N")); var tagHelperOutput = new TagHelperOutput( tagName: string.Empty, attributes, getChildContentAsync: (_, _) => Task.FromResult(new DefaultTagHelperContent() as TagHelperContent)); var componentTagHelper = new ComponentTagHelper { ComponentType = componentType, RenderMode = renderMode, Parameters = parameters, ViewContext = new ViewContext { HttpContext = httpContext } }; await componentTagHelper.ProcessAsync(tagHelperContext, tagHelperOutput); httpContextFactory.Dispose(httpContext); return(tagHelperOutput.Content); }