Open Closed

Unit test for Identity microservice #5285


User avatar
0
AbpRaven created

Hi. I have problem with testing of Identity microservice. I got an issue .Reflection.DefaultConstructorFinder' on type 'Volo.Abp.SettingManagement.SettingManagementStore' can be invoked with the available services and parameters: Cannot resolve parameter 'Volo.Abp.SettingManagement.ISettingRepository settingRepository' of constructor 'Void .ctor(Volo.Abp.SettingManagement.ISettingRepository, Volo.Abp.Guids.IGuidGenerator, Volo.Abp.Caching.IDistributedCache`1[Volo.Abp.SettingManagement.SettingCacheItem], Volo.Abp.Settings.ISettingDefinitionManager)'.

But in another microservice I did not get this issue. For exapmle in another microservice we mock settings: and when we mock our settings manager:

But in identity microservice it is not working and I do not understand what is wrong

How we can fix the problem? please help

  • ABP Framework version: v6.0.0
  • UI type: Angular
  • DB provider: EF Core

10 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Could you provide the full steps to reproduce the problem? thanks.

  • User Avatar
    0
    AbpRaven created

    Hi,

    Could you provide the full steps to reproduce the problem? thanks.

    I have create a new (first) test in identity microservice

    public sealed class SampleAppService_Tests : IdentityServiceApplicationTestBase
    {
        private readonly IUserAppService _userAppService;
        private ISettingManagementStore _abpSettingStore;
    
        protected override void BeforeAddApplication(IServiceCollection services)
        {
            _abpSettingStore = Substitute.For<ISettingManagementStore>();
            services.AddSingleton(_abpSettingStore);
            base.BeforeAddApplication(services);
        }
        
        public SampleAppService_Tests()
        {
            _userAppService = GetRequiredService<IUserAppService>();
        }
    
        [Fact]
        public async Task Test()
        {
            var test = await _userAppService.CreateUser(new UserCreateInput()
            {
                UserName = "TestUserName",
                Email = "test@test.com"
            }, ERole.External);
        }
    }
    

    And got this error Autofac.Core.DependencyResolutionException: An exception was thrown while activating Volo.Abp.Settings.DefaultValueSettingValueProvider -> Volo.Abp.SettingManagement.SettingStore -> Volo.Abp.SettingManagement.SettingManagementStore. ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Volo.Abp.SettingManagement.SettingManagementStore' can be invoked with the available services and parameters: Cannot resolve parameter 'Volo.Abp.SettingManagement.ISettingRepository settingRepository' of constructor 'Void .ctor(Volo.Abp.SettingManagement.ISettingRepository, Volo.Abp.Guids.IGuidGenerator, Volo.Abp.Caching.IDistributedCache1[Volo.Abp.SettingManagement.SettingCacheItem], Volo.Abp.Settings.ISettingDefinitionManager)'.`

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try:

    public sealed class SampleAppService_Tests : IdentityServiceDomainTestBase
    {
        private readonly IUserAppService _userAppService;
        private ISettingManagementStore _abpSettingStore;
    
        protected override void BeforeAddApplication(IServiceCollection services)
        {
            _abpSettingStore = Substitute.For<ISettingManagementStore>();
            services.AddSingleton(_abpSettingStore);
            base.BeforeAddApplication(services);
        }
        
        public SampleAppService_Tests()
        {
            _userAppService = GetRequiredService<IUserAppService>();
        }
    
        [Fact]
        public async Task Test()
        {
            var test = await _userAppService.CreateUser(new UserCreateInput()
            {
                UserName = "TestUserName",
                Email = "test@test.com"
            }, ERole.External);
        }
    }
    
  • User Avatar
    0
    AbpRaven created

    hi, Why should we use IdentityServiceDomainTestBase for application layer tests?

  • User Avatar
    0
    AbpRaven created

    If I use IdentityServiceDomainTestBase I got another error: `Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'AMSuisse.IdentityService.Interfaces.User.IUserAppService' has not be...

    Autofac.Core.Registration.ComponentNotRegisteredException The requested service 'AMSuisse.IdentityService.Interfaces.User.IUserAppService' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.`

  • User Avatar
    0
    AbpRaven created

    Hi,

    You can try:

    public sealed class SampleAppService_Tests : IdentityServiceDomainTestBase 
    { 
        private readonly IUserAppService _userAppService; 
        private ISettingManagementStore _abpSettingStore; 
     
        protected override void BeforeAddApplication(IServiceCollection services) 
        { 
            _abpSettingStore = Substitute.For<ISettingManagementStore>(); 
            services.AddSingleton(_abpSettingStore); 
            base.BeforeAddApplication(services); 
        } 
         
        public SampleAppService_Tests() 
        { 
            _userAppService = GetRequiredService<IUserAppService>(); 
        } 
     
        [Fact] 
        public async Task Test() 
        { 
            var test = await _userAppService.CreateUser(new UserCreateInput() 
            { 
                UserName = "TestUserName", 
                Email = "test@test.com" 
            }, ERole.External); 
        } 
    } 
    

    If I IdentityServiceDomainTestBase I can test only managers. But I need to test applications services.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    We will check it.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try:

    public abstract class IdentityServiceApplicationTestBase : IdentityServiceTestBase<IdentityServiceApplicationTestModule>
    {
        protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
        {
            options.AddDataMigrationEnvironment();
        }
    }
    
    public class IdentityServiceApplicationTestModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBackgroundJobOptions>(options =>
            {
                options.IsJobExecutionEnabled = false;
            });
            
            Configure<AbpBackgroundWorkerOptions>(options =>
            {
                options.IsEnabled = false;
            });
        }
    }
    
    • Add Volo.Abp.SettingManagement.EntityFrameworkCore to IdentityService.EntityFrameworkCore.Tests project
    [DependsOn(
        typeof(IdentityServiceTestBaseModule),
        typeof(IdentityServiceEntityFrameworkCoreModule),
        typeof(AbpSettingManagementEntityFrameworkCoreModule),
        typeof(AbpEntityFrameworkCoreSqliteModule)
    )]
    public class IdentityServiceEntityFrameworkCoreTestModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var sqliteConnection = CreateDatabaseAndGetConnection();
    
            Configure<AbpDbContextOptions>(options =>
            {
                options.Configure<IdentityServiceDbContext>(c =>
                {
                    c.DbContextOptions.UseSqlite(sqliteConnection);
                });
                
                options.Configure<SettingManagementDbContext>(c =>
                {
                    c.DbContextOptions.UseSqlite(sqliteConnection);
                });
            });
        }
    
        private static SqliteConnection CreateDatabaseAndGetConnection()
        {
            var connection = new SqliteConnection("Data Source=:memory:");
            connection.Open();
    
            new IdentityServiceDbContext(
                new DbContextOptionsBuilder<IdentityServiceDbContext>().UseSqlite(connection).Options
            ).GetService<IRelationalDatabaseCreator>().CreateTables();
            
            new SettingManagementDbContext(
                new DbContextOptionsBuilder<SettingManagementDbContext>().UseSqlite(connection).Options
            ).GetService<IRelationalDatabaseCreator>().CreateTables();
    
            return connection;
        }
    }
    
  • User Avatar
    0
    AbpRaven created

    hi Thank you so much. I have tried you solution and it works. Btw I have resolve this issue another way. But one smole question what is the method options.AddDataMigrationEnvironment() ?? Because right now i do not have it

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    This is added after 7.0, you can ignore it

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09