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 new file mode 100644 index 0000000000..df4265a4a0 --- /dev/null +++ b/docs/en/Community-Articles/2022-01-10-How-to-test-Blazor-components-in-ABP/POST.md @@ -0,0 +1,111 @@ +# How to Test Blazor Components in ABP + +## Source Code + +You can find the source of the example solution used in this article [here](https://github.com/abpframework/abp-samples/tree/master/BlazorPageUniTest). + + +In this article, I will use [bUnit](https://github.com/bUnit-dev/bUnit) for a simple test of a Blazor component. + +## Getting Started + +Use the ABP CLI to create a blazor app + +`abp new BookStore -t app -u blazor` + +Then add the `BookStore.Blazor.Tests` xunit test project to the solution, and add [bUnit](https://github.com/bUnit-dev/bUnit) package and `ProjectReference` to the test project. + +The contents of `BookStore.Blazor.Tests.csproj` +```xml + + + + net6.0 + enable + false + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + +``` + +Create `BookStoreBlazorTestModule` that depends on `AbpAspNetCoreComponentsModule` and `BookStoreEntityFrameworkCoreTestModule`. + +```cs +[DependsOn( + typeof(AbpAspNetCoreComponentsModule), + typeof(BookStoreEntityFrameworkCoreTestModule) +)] +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`. + +```cs +public abstract class BookStoreBlazorTestBase : BookStoreTestBase +{ + protected virtual TestContext CreateTestContext() + { + var testContext = new TestContext(); + testContext.Services.AddFallbackServiceProvider(ServiceProvider); + foreach (var service in ServiceProvider.GetRequiredService().Services) + { + testContext.Services.Add(service); + } + testContext.Services.AddBlazorise().AddBootstrap5Providers().AddFontAwesomeIcons(); + return testContext; + } +} +``` + +Finally, we add an `Index_Tests` class to test the `Index` component. + +```cs +public class Index_Tests : BookStoreBlazorTestBase +{ + [Fact] + public void Index_Test() + { + using (var ctx = CreateTestContext()) + { + // Act + var cut = ctx.RenderComponent(); + + // 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(); + } + } +} +``` + +## Reference document + +https://github.com/bUnit-dev/bUnit + +https://docs.microsoft.com/en-us/aspnet/core/blazor/test diff --git a/docs/en/Tutorials/Part-10.md b/docs/en/Tutorials/Part-10.md index 563de95d68..58ed534746 100644 --- a/docs/en/Tutorials/Part-10.md +++ b/docs/en/Tutorials/Part-10.md @@ -377,7 +377,7 @@ namespace Acme.BookStore.Books //Prepare a query to join books and authors var query = from book in queryable - join author in _authorRepository on book.AuthorId equals author.Id + join author in await _authorRepository.GetQueryableAsync() on book.AuthorId equals author.Id where book.Id == id select new { book, author }; @@ -400,7 +400,7 @@ namespace Acme.BookStore.Books //Prepare a query to join books and authors var query = from book in queryable - join author in _authorRepository on book.AuthorId equals author.Id + join author in await _authorRepository.GetQueryableAsync() on book.AuthorId equals author.Id select new {book, author}; //Paging diff --git a/framework/src/Volo.Abp.EntityFrameworkCore.PostgreSql/Volo/Abp/EntityFrameworkCore/PostgreSql/AbpEntityFrameworkCorePostgreSqlModule.cs b/framework/src/Volo.Abp.EntityFrameworkCore.PostgreSql/Volo/Abp/EntityFrameworkCore/PostgreSql/AbpEntityFrameworkCorePostgreSqlModule.cs index 596149722d..8fffb8359c 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore.PostgreSql/Volo/Abp/EntityFrameworkCore/PostgreSql/AbpEntityFrameworkCorePostgreSqlModule.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore.PostgreSql/Volo/Abp/EntityFrameworkCore/PostgreSql/AbpEntityFrameworkCorePostgreSqlModule.cs @@ -1,5 +1,4 @@ -using System; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.EntityFrameworkCore.DistributedEvents; using Volo.Abp.Guids; using Volo.Abp.Modularity; @@ -11,12 +10,6 @@ namespace Volo.Abp.EntityFrameworkCore.PostgreSql; )] public class AbpEntityFrameworkCorePostgreSqlModule : AbpModule { - public override void PreConfigureServices(ServiceConfigurationContext context) - { - // https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic - AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); - } - public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index 1f55bce770..8d519784dc 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -122,6 +122,12 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, if (provider != null) { modelBuilder.SetDatabaseProvider(provider.Value); + + if (provider.Value == EfCoreDatabaseProvider.PostgreSql) + { + // https://www.npgsql.org/efcore/release-notes/6.0.html#opting-out-of-the-new-timestamp-mapping-logic + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + } } }