diff --git a/docs/en/Community-Articles/2022-01-10-How-to-test-Blazor-components-in-ABP/POST.md b/docs/en/Community-Articles/2022-01-10-How-to-test-Blazor-components-in-ABP/POST.md index df4265a4a0..d87ee8cc48 100644 --- a/docs/en/Community-Articles/2022-01-10-How-to-test-Blazor-components-in-ABP/POST.md +++ b/docs/en/Community-Articles/2022-01-10-How-to-test-Blazor-components-in-ABP/POST.md @@ -20,21 +20,22 @@ The contents of `BookStore.Blazor.Tests.csproj` - net6.0 + net8.0 enable + false - - - - - + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -46,6 +47,7 @@ The contents of `BookStore.Blazor.Tests.csproj` + ``` Create `BookStoreBlazorTestModule` that depends on `AbpAspNetCoreComponentsModule` and `BookStoreEntityFrameworkCoreTestModule`. @@ -63,7 +65,7 @@ public class BookStoreBlazorTestModule : AbpModule Create a `BookStoreBlazorTestBase` class and add the `CreateTestContext` method. The `CreateTestContext` have key code. -It uses ABP's `ServiceProvider` as a fallback `ServiceProvider` and add all ABP's services to the `TestContext`. +It creates a `AutofacServiceProvider` and add all ABP's services to the `TestContext`. ```cs public abstract class BookStoreBlazorTestBase : BookStoreTestBase @@ -71,12 +73,23 @@ public abstract class BookStoreBlazorTestBase : BookStoreTestBase().Services) + var blazorise = testContext.JSInterop.SetupModule("./_content/Blazorise/utilities.js?v=1.5.1.0"); + blazorise.SetupVoid("log", _ => true); + + testContext.Services.UseServiceProviderFactory(serviceCollection => { - testContext.Services.Add(service); - } + foreach (var service in ServiceProvider.GetRequiredService().Services) + { + serviceCollection.Add(service); + } + var containerBuilder = new ContainerBuilder(); + containerBuilder.Populate(serviceCollection); + return new AutofacServiceProvider(containerBuilder.Build()); + }); + testContext.Services.AddBlazorise().AddBootstrap5Providers().AddFontAwesomeIcons(); + testContext.Services.Replace(ServiceDescriptor.Transient()); + return testContext; } } @@ -87,19 +100,18 @@ Finally, we add an `Index_Tests` class to test the `Index` component. ```cs public class Index_Tests : BookStoreBlazorTestBase { - [Fact] +[Fact] public void Index_Test() { - using (var ctx = CreateTestContext()) - { - // Act - var cut = ctx.RenderComponent(); + // Arrange + var ctx = CreateTestContext(); - // Assert - cut.Find(".lead").InnerHtml.Contains("Welcome to the application. This is a startup project based on the ABP framework. For more information, visit abp.io.").ShouldBeTrue(); + // Act + var cut = ctx.RenderComponent(); - cut.Find("#username").InnerHtml.Contains("Welcome admin").ShouldBeTrue(); - } + // Assert + cut.Find(".lead").InnerHtml.Contains("Welcome to the application. This is a startup project based on the ABP framework. For more information, visit abp.io.").ShouldBeTrue(); + cut.Find("#username").InnerHtml.Contains("Welcome admin").ShouldBeTrue(); } } ```