UseInMemoryDatabase context isn't being shared across unit tests

asp.net-core c# entity-framework-core sql xunit

Question

I'm using Entity Framework Core 2.0-preview1 with InMemory 2.0-preview1. Each unit test class inherits a disposable class that creates a new in memory database that its parents can use.

    public Constructor()
    {
        var services = new ServiceCollection();

        services.AddEntityFrameworkInMemoryDatabase()
            .AddDbContext<DBContext>(o => o.UseInMemoryDatabase("Test"));

        var serviceProvider = services.BuildServiceProvider();

        Context = serviceProvider.GetRequiredService<DBContext>();
    }

The issue with giving the database a name is that it cannot be shared across multiple tests and thus each test creates a new context resulting in each unit test lasting a few seconds which is unacceptable for my build server. I cant find much documentation on why this was changed in 2.0 or how to get past this. I've tried using the new .UseTransientInMemoryDatabase but this appears to change nothing. Thanks in advance.

1
0
5/16/2017 3:33:42 PM

Accepted Answer

I used a xUnit fixture to provide all my test instances with the save database context. That way I avoid the context creation overhead on each test which speeds the build server up by a large margin.

0
8/9/2017 3:38:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow