Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Automated Test Data #9506


User avatar
0
fawad29 created

I am aware that the framework uses in-memory database to run tests. My issue is that we keep some configuration data for each tenant in tenant's database. Now in order to create test, we need to manually create that configuration data in the code for each tenant and then create tests and run them. This is very manual process. Is there a way, where we continue to use in-memory database in tests but can retrieve the configuration data from the actual database?

Product Details Product version is 8.0.0. ABP Framework is the one which came with version 8.0.0. ASP.NET Core MVC & jQuery Project

Thanks


13 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    You can create a service in the unit test, it can use EF or abo or dapper to read the real database.

  • User Avatar
    0
    fawad29 created

    Hi

    Can you provide some example code for this as it will help me to go in the right direction?

  • User Avatar
    0
    maliming created
    Support Team

    hi

    This service is just to read your database to get configuration data.(`EF or abo.net or dapper)

    Of course you can add this service to the dependency injection container.(ITransientDependency or ISingletonDependency )

  • User Avatar
    0
    fawad29 created

    Will I declare that service in Nebula.Tests project?

  • User Avatar
    0
    maliming created
    Support Team

    hi @learner29

    This service should only exist in unit tests.

  • User Avatar
    0
    fawad29 created

    Hi @maliming,

    I have done as you suggested but I am getting the following exception.

     Message: 
        System.ObjectDisposedException : Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
        Object name: 'NebulaDbContext'.
      Stack Trace: 
        DbContext.CheckDisposed()
        DbContext.get_DbContextDependencies()
        DbContext.get_Model()
        InternalDbSet`1.get_EntityType()
        InternalDbSet`1.get_EntityQueryable()
        IQueryable.get_Provider()
        Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
        EthnicityAppService_Tests.GetAll() line 1548
        MyAppService_Tests.GetAll_Test() line 234
        --- End of stack trace from previous location where exception was thrown
    
    

    My ethnicity class is as follows:

    public class EthnicityAppService_Tests : AppTestBase
    {
    private readonly IRepository<Ethnicity> _ethnicityRepositroy;
    
    public EthnicityAppService_Tests()
            {
                
                _ethnicityRepositroy = Resolve<IRepository<Ethnicity>>();
    
            }
    public async Task<List<EthnicitiesListDto>> GetAll()
            {
                var ethnicities = from ethnicity in _ethnicityRepositroy.GetAll()
    
                                  select new EthnicitiesListDto
                                  {
                                      Id = ethnicity.Id,
                                      Text = ethnicity.Text,
                                      Code = ethnicity.Code,
                                      TenantId = ethnicity.TenantId
                                  };
                return ethnicities.ToList();
            }
    
    }
    

    And I am calling above test from another class which requires data from the ethnicity table, I get error in GeteAll_Test() where I call the function.

    public class MyAppService_Tests : AppTestBase
    {
    private readonly EthnicityAppService_Tests _ethnicityAppServiceTest;
    
        public MyAppService_Tests()
        {
        _ethnicityAppServiceTest = Resolve<EthnicityAppService_Tests>();
        }
        
        [Fact]
        public async Task GetAll_Test()
        {
        //I get error, the control goes into the function and then returns above error.
         List<EthnicitiesListDto> ethnicities =  await _ethnicityAppServiceTest.GetAll();
        }
    }
    
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Please try create a unit of work. See https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#irepository-getall-method

    using (var uow = _unitOfWorkManager.Begin())
    {
    
    	await uow.CompleteAsync();
    }
    
  • User Avatar
    0
    fawad29 created

    Hi @maliming,

    I have tried your method and I don't get the error now. However, no data is returned from the database, even though data exists in the database.

    public class EthnicityAppService_Tests : AppTestBase
    {
    private readonly IRepository<Ethnicity> _ethnicityRepositroy;
    private readonly IUnitOfWorkManager _unitOfWorkManager;
    
    public EthnicityAppService_Tests()
            {
                
                _ethnicityRepositroy = Resolve<IRepository<Ethnicity>>();
                _unitOfWorkManager = Resolve<IUnitOfWorkManager>();
    
            }
    public async Task<List<EthnicitiesListDto>> GetAll()
            {
               List<EthnicitiesListDto> a = new List<EthnicitiesListDto>();
                using (var uow = _unitOfWorkManager.Begin())
                {
                    var test = from ethnicity in _ethnicityRepositroy.GetAll()
                               select new EthnicitiesListDto
                               {
                                   Id = ethnicity.Id,
                                   Text = ethnicity.Text,
                                   Code = ethnicity.Code,                               
                                   TenantId = ethnicity.TenantId
                               };
                    a = test.ToList();//No Data is apppearing, event though database has the data
                    await uow.CompleteAsync();
                }
    
                return a;
            }
    
    }
    

    No data is appearing event though there is data in the database. I even tried by using tenant id e.g

    from ethnicity in _ethnicityRepositroy.GetAll().Where(e => e.TenantId == 3) but of no use.

    public class MyAppService_Tests : AppTestBase
    {
    private readonly EthnicityAppService_Tests _ethnicityAppServiceTest;
    
        public MyAppService_Tests()
        {
        _ethnicityAppServiceTest = Resolve<EthnicityAppService_Tests>();
        }
        
        [Fact]
        public async Task GetAll_Test()
        {
        //I get error, the control goes into the function and then returns above error.
         List<EthnicitiesListDto> ethnicities =  await _ethnicityAppServiceTest.GetAll();
        }
    }
    

    Thanks

  • User Avatar
    0
    maliming created
    Support Team

    hi

    You can try switch to host side. or disable muti-tenancy(IMayHaveTenant ).https://aspnetboilerplate.com/Pages/Documents/Data-Filters#disable-filters

    using (_unitOfWorkManager.Current.SetTenantId(null))
    {
        using (var uow = _unitOfWorkManager.Begin())
        {
            var test = from ethnicity in _ethnicityRepositroy.GetAll()
                       select new EthnicitiesListDto
                       {
                           Id = ethnicity.Id,
                           Text = ethnicity.Text,
                           Code = ethnicity.Code,                               
                           TenantId = ethnicity.TenantId
                       };
            a = test.ToList();//No Data is apppearing, event though database has the data
            await uow.CompleteAsync();
        }
    }
    
  • User Avatar
    0
    fawad29 created

    Hi @maliming

    We need to get the data for a particular tenant, is that not possible?

    We also get a null exception, see the attached.

  • User Avatar
    0
    maliming created
    Support Team

    hi

    Sorry,The order is wrong.

    using (var uow = _unitOfWorkManager.Begin())
    {
        using (_unitOfWorkManager.Current.SetTenantId(null))
        {
            var test = from ethnicity in _ethnicityRepositroy.GetAll()
                       select new EthnicitiesListDto
                       {
                           Id = ethnicity.Id,
                           Text = ethnicity.Text,
                           Code = ethnicity.Code,                               
                           TenantId = ethnicity.TenantId
                       };
            a = test.ToList();//No Data is apppearing, event though database has the data
            await uow.CompleteAsync();
        }
    }
    
    
  • User Avatar
    0
    fawad29 created

    Hi,

    I have tried this order and I do not receive an exception. However, the data is still not being returned. I have emailed my solution for a different ticket (9631), you can check that solution and see if you can get the data.

  • User Avatar
    0
    maliming created
    Support Team

    hi @learner29

    Has your problem been solved? If not, you can also send me an email. [email protected]