您好,我的应用程序中包含一些集成测试。在实例化测试类时,我正在使用以下类来准备数据库:
internal static class DatabaseTestPreparation
{
private const string TestDatabaseName = "TestDatabase";
internal static IServiceCollection ConfigureTestDatabases(this IServiceCollection services) =>
services
.PrepareTestDatabaseContext<EventStoreContext>()
.PrepareTestDatabaseContext<GrantContext>();
private static IServiceCollection PrepareTestDatabaseContext<T>(this IServiceCollection services)
where T : DbContext
{
var descriptor = services.SingleOrDefault(service =>
service.ServiceType == typeof(DbContextOptions<T>));
if (!(descriptor is null))
{
services
.Remove(descriptor);
}
services
.AddDbContext<T>(options =>
options.UseInMemoryDatabase(TestDatabaseName))
.Seed<T>();
return services;
}
private static IServiceCollection Seed<T>(this IServiceCollection services) where T : DbContext
{
var serviceProvider = services.BuildServiceProvider();
var dbContext = serviceProvider
.GetService(typeof(T)) as T;
dbContext
?.Database
.EnsureCreated();
return services;
}
}
我的DbContext类中也包含一些种子逻辑。例如:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
CreateConstraints(modelBuilder);
SeedInitialData(modelBuilder);
}
private static void CreateConstraints(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PermissionEntity>()
.HasIndex(permission => permission.Code)
.IsUnique();
modelBuilder.Entity<ApplicationEntity>()
.HasIndex(app => app.Name)
.IsUnique();
}
private static void SeedInitialData(ModelBuilder modelBuilder)
{
var application = new ApplicationEntity
{
...
};
modelBuilder.Entity<ApplicationEntity>()
.HasData(application);
var managerRole = new RoleEntity
{
...
};
modelBuilder.Entity<RoleEntity>()
.HasData(managerRole);
var permission = new PermissionEntity
{
...
};
modelBuilder.Entity<PermissionEntity>()
.HasData(permission);
var permissionRoleAssignment = new PermissionRoleAssignmentEntity
{
...
};
modelBuilder.Entity<PermissionRoleAssignmentEntity>()
.HasData(permissionRoleAssignment);
var principal = new PrincipalEntity
{
...
};
modelBuilder.Entity<PrincipalEntity>()
.HasData(principal);
var principalRoleAssignment = new PrincipalRoleAssignmentEntity
{
...
};
modelBuilder.Entity<PrincipalRoleAssignmentEntity>()
.HasData(principalRoleAssignment);
}
上述这些方法可以确保正确执行,因为在运行测试时,我已通过调试器对其进行了检查。问题是,尽管我已经在DbContext类中编写了种子方法,但是当某些测试方法正在运行时,似乎没有数据在测试方法中使用的DbContext中。如何使用EF Core InMemoryProvider正确播种数据?我在做什么错了?