Base solution for your next web application
Open Closed

Unit tests with multiple db contexts #7198


User avatar
0
Healthbit created

I am using multiple db contexts. I am getting following error when I run existing unit tests: [24/06/2019 02:16:18 PM Informational] [xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.1 (64-bit .NET Core 4.6.27617.05) [24/06/2019 02:16:19 PM Informational] [xUnit.net 00:00:00.88] Starting: XXXXDemo.Tests [24/06/2019 02:16:23 PM Error] [xUnit.net 00:00:04.40] XXXXDemo.Tests.Features.Features_Tests.Should_Not_Create_User_More_Than_Allowed_Count [FAIL] [24/06/2019 02:16:23 PM Informational] [xUnit.net 00:00:04.40] Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'no such table: AbpEditions'.

** How to setup existing unit tests project and use different db contexts?**


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @hb

    Could you share your unit test class and your second dbContext ?

    Thanks.

  • User Avatar
    0
    Healthbit created

    @ismcagdas even all the default tests are not woking. All tests are ending with Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'no such table: xxxx'.

    CommerceDbContext.cs

    using Abp.IdentityServer4;
    using Abp.Zero.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using CommerceDemo.Authorization.Roles;
    using CommerceDemo.Authorization.Users;
    using CommerceDemo.Chat;
    using CommerceDemo.Editions;
    using CommerceDemo.Friendships;
    using CommerceDemo.MultiTenancy;
    using CommerceDemo.MultiTenancy.Accounting;
    using CommerceDemo.MultiTenancy.Payments;
    using CommerceDemo.Storage;
    
    namespace CommerceDemo.EntityFrameworkCore
    {
        public class CommerceDbContext : AbpZeroDbContext<Tenant, Role, User, CommerceDbContext>, IAbpPersistedGrantDbContext
        {
            /* Define an IDbSet for each entity of the application */
    
            public virtual DbSet<CommerceDemo.Main.Items.Item> Items { get; set; }
     
    
    
    		public virtual DbSet<PersistedGrantEntity> PersistedGrants { get; set; }
            public CommerceDbContext(DbContextOptions<CommerceDbContext> options)
                : base(options)
            {
                
            }
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                 
                modelBuilder.ConfigurePersistedGrantEntity();
            }
        }
    }
    
    

    ItemAppService_Tests.cs

    using System.Globalization;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp;
    using Abp.Application.Services.Dto;
    using Abp.Localization;
    using CommerceDemo.Main.Items;
    using CommerceDemo.Localization;
    using CommerceDemo.Localization.Dto;
    using CommerceDemo.Migrations.Seed.Host;
    using CommerceDemo.Test.Base;
    using Shouldly;
    using Xunit;
    
    namespace CommerceDemo.Tests.Main.Items
    {
        // ReSharper disable once InconsistentNaming
        public class ItemAppService_Tests : AppTestBase
        {
            private readonly IItemAppService _ItemAppService;
            private readonly bool _multiTenancyEnabled  = CommerceDemoConsts.MultiTenancyEnabled;
    
            public ItemAppService_Tests()
            {
                if (_multiTenancyEnabled)
                {
                    LoginAsHostAdmin();
                }
                else
                {
                    LoginAsDefaultTenantAdmin();
                }
    
    			_ItemAppService = Resolve<IItemAppService>();
     
            }
    
            [Fact]
            public async Task Test_GetItems()
            {
                //Act
                var output = await _ItemAppService.GetItems( new CommerceDemo.Main.Items.Dto.GetItemsInput());
    
                //Assert
                output.Items.Count.ShouldBeGreaterThan(0); ;
            }
    
        
        }
    }
    
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You should create a similar class like AppTestBase which will be using your second database. If that doesn't work for you, please send a sample project to [email protected] and let us check it for you.

    Thanks.