当前位置:Gxlcms > 数据库问题 > 将Abp的UnitTest中的InMemory改为SQLite in memory

将Abp的UnitTest中的InMemory改为SQLite in memory

时间:2021-07-01 10:21:17 帮助过:21人阅读

添加nuget包

Microsoft.EntityFrameworkCore.Sqlite

添加ServiceCollectionRegistrarSqlite

 public static class ServiceCollectionRegistrarSqlite
    {
        public static void Register(IIocManager iocManager)
        {
            var services = new ServiceCollection();

            IdentityRegistrar.Register(services);

            services.AddEntityFrameworkSqlite();

            var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(iocManager.IocContainer, services);

            var builder = new DbContextOptionsBuilder<DeviceManageSystemDbContext>();


            var inMemorySqlite = new SqliteConnection("Data Source=:memory:");
            builder.UseSqlite(inMemorySqlite);

            iocManager.IocContainer.Register(
                Component
                    .For<DbContextOptions<DeviceManageSystemDbContext>>()
                    .Instance(builder.Options)
                    .LifestyleSingleton()
            );

            inMemorySqlite.Open();
            new DeviceManageSystemDbContext(builder.Options).Database.EnsureCreated();
        }
    }

添加DeviceManageSystemTestModuleSqlite

复制DeviceManageSystemTestModule,修改

 [DependsOn(
        typeof(DeviceManageSystemApplicationModule),
        typeof(DeviceManageSystemEntityFrameworkModule),
        typeof(AbpTestBaseModule)
        )]
    public class DeviceManageSystemTestModuleSqlite : AbpModule
    {
     public override void Initialize()
        {
            ServiceCollectionRegistrarSqlite.Register(IocManager);
        }
    }

修改DeviceManageSystemTestBase

public abstract class DeviceManageSystemTestBase : AbpIntegratedTestBase<DeviceManageSystemTestModuleSqlite>
{

}

将Abp的UnitTest中的InMemory改为SQLite in memory

标签:options   unittest   contain   ros   initial   contex   device   created   systemd   

人气教程排行