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?

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

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;
            }
        }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on October 05, 2026, 11:32
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.