Open Closed

How to initialize unit tests to use in memory database for SettingManagementDbContext #1804


User avatar
0
LW created
  • ABP Framework version: 4.4.0
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace:
  • Autofac.Core.DependencyResolutionException: 'An exception was thrown while activating Volo.Abp.SettingManagement.EntityFrameworkCore.SettingManagementDbContext -> λ:Microsoft.EntityFrameworkCore.DbContextOptions`1[[Volo.Abp.SettingManagement.EntityFrameworkCore.SettingManagementDbContext, Volo.Abp.SettingManagement.EntityFrameworkCore, Version=4.4.0.0, Culture=neutral, PublicKeyToken=null]].'

ArgumentNullException: Value cannot be null. Arg_ParamName_Name (ConnectionString)

  • Steps to reproduce the issue:"

I created a new console application with the cli generator. After that I created a module that depends on the settings module. Apparently this brings a dependency to SettingManagementDbContext. My intend was to create a unit test module to test the module and I mimiced the Domain.test -project initilaziation from another application. However this time the initilization wants to use the real database instead of the in memory -database for SettingManagementDbContext.

What should I do in the test base initialization to make a generic module to use in memory-database?


1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    your domain service should depend on the EntityFrameworkCore.Test project. and your EntityFrameworkCore.Test project module class should be like this

    You can create a new project from module template, to see how it works

    abp new Acme.MyProModule -t module-pro -csf
    
    [DependsOn(
            typeof(MyProjectNameTestBaseModule),
            typeof(MyProjectNameEntityFrameworkCoreModule),
            typeof(AbpEntityFrameworkCoreSqliteModule)
            )]
        public class MyProjectNameEntityFrameworkCoreTestModule : AbpModule
        {
            public override void ConfigureServices(ServiceConfigurationContext context)
            {
                var sqliteConnection = CreateDatabaseAndGetConnection();
    
                Configure<AbpDbContextOptions>(options =>
                {
                    options.Configure(abpDbContextConfigurationContext =>
                    {
                        abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection);
                    });
                });
            }
            
            private static SqliteConnection CreateDatabaseAndGetConnection()
            {
                var connection = new SqliteConnection("Data Source=:memory:");
                connection.Open();
    
                new MyProjectNameDbContext(
                    new DbContextOptionsBuilder<MyProjectNameDbContext>().UseSqlite(connection).Options
                ).GetService<IRelationalDatabaseCreator>().CreateTables();
                
                return connection;
            }
        }
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11