测试ASP.NET Core应用程序时,如何使用InMemory数据库使用EF Core将数据播种到DbContext?

时间:2020-10-17 18:31:04

标签: c# asp.net asp.net-core entity-framework-core integration-testing

您好,我的应用程序中包含一些集成测试。在实例化测试类时,我正在使用以下类来准备数据库:

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正确播种数据?我在做什么错了?

0 个答案:

没有答案