1

We have a .NET Core 2.2 project using ASP.NET Boilerplate 4.10.1.

This project has a huge db context that takes way too long to load for each integration test we're running, so we'd like to see how it'd go with a shared context between tests.

Unfortunately, we're unable to figure out how to combine XUnit collection fixtures with ABP, and found no documentation on the matter.

Some of our code, if it helps:

public abstract class EcfTestBase : AbpIntegratedTestBase<EcfTestBaseModule>
{
    protected EcfTestBase()
    {
        AbpSession.TenantId = null;
        UsingDbContext(context =>
        {
            NormalizeDbContext(context);
            // Seed initial data for host
            SeedHelper.SeedHostDb(context);
        });

        // Seed initial data for default tenant
        AbpSession.TenantId = 1;
        UsingDbContext(context =>
        {
            NormalizeDbContext(context);
        });

        LoginAsDefaultTenantAdmin();

        void NormalizeDbContext(EcfDbContext context)
        {
            context.EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
            context.EventBus = NullEventBus.Instance;
            context.SuppressAutoSetTenantId = true;
        }
    }

Integration test:

public class Reg0000Controller_Tests : EcfTestBase
{
    private readonly Reg0000AppService _Reg0000AppService;

    public Reg0000Controller_Tests()
    {
        _Reg0000AppService = Resolve<Reg0000AppService>();
    }

    [Fact]
    public async Task Reg0000_Create()
    {
        using (var controller = new Reg0000Controller(_Reg0000AppService))
        {
            var result = await controller.Create(new Reg0000Dto
            {
                CenarioId = 1,                        
                Nome = "Empresa Teste"
            });

            result.Nome.ShouldBe("Empresa Teste");
            await UsingDbContextAsync(async context =>
            {
                var reg0000 = await context.Reg0000.FirstOrDefaultAsync(u => u.Nome == "Empresa Teste");
                reg0000.ShouldNotBeNull();
                reg0000.CreatorUserId.ShouldNotBeNull();
                reg0000.CreationTime.ShouldNotBeNull();
                reg0000.LastModifierUserId.ShouldBeNull();
            });
        }
Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
  • 2
    Don't use a huge DbContext. It's as simple as that. A DbContext is essentially a Unit of Work and should *only* contain the DbSets needed for it to do its job. If you're into DDD think about bounded contexts. You should read [No need for repositories and unit of work with Entity Framework Core](https://gunnarpeipman.com/ef-core-repository-unit-of-work/) too. ABP generates some pretty horrible EF and data access code. – Panagiotis Kanavos Dec 17 '19 at 13:26

0 Answers0