Tests : Entity Framework Core Layer¶
-
Do create a concrete implementation of the <DomainModelName>Repository_Tests
class. - Do create an unit test for each domain model repository with <DomainModelName>Repository_Tests name.
In this case it is
IdentityUserRepository_Tests
.public class IdentityUserRepository_Tests : IdentityUserRepository_Tests<IdentityEntityFrameworkCoreTestModule> { }
-
Do reuse the existing DbContext which defined in the GridLab.<ModuleName>.EntityFrameworkCore layer with a in-memory SQLite database option for Entity Framework Core tests.
The idea of this post is to use SQLite as the database. SQLite also has an option to run fully in-memory (e.g. without writing data on disk).
This way, you use a relational database but in memory, which is great for unit testing.
[DependsOn(
typeof(IdentityTestBaseModule),
typeof(AbpPermissionManagementEntityFrameworkCoreModule),
typeof(IdentityEntityFrameworkCoreModule)
)]
public class IdentityEntityFrameworkCoreTestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAlwaysDisableUnitOfWorkTransaction();
var sqliteConnection = CreateDatabaseAndGetConnection();
Configure<AbpDbContextOptions>(options =>
{
options.Configure(configurationContext =>
{
configurationContext.DbContextOptions.UseSqlite(sqliteConnection);
});
});
}
private static SqliteConnection CreateDatabaseAndGetConnection()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
new IdentityExtendedDbContext(
new DbContextOptionsBuilder<IdentityExtendedDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();
... add extra context if needed
new PermissionManagementDbContext(
new DbContextOptionsBuilder<PermissionManagementDbContext>().UseSqlite(connection).Options
).GetService<IRelationalDatabaseCreator>().CreateTables();
return connection;
}
}
SQLite can run in-memory using the connection string DataSource=:memory
. When the connection is opened, a new database is created in memory. This database is destroyed when the connection is closed.
This means, you must keep the connection open until the test ends.