· 1 min read
Using In Memory Database Context for an Integration Test
I had a requirement to access the data of an in memory database context for an integration test.
So for example some seeded data created in the CustomWebApplicationFactory needed to be accessed as part of an integration test.
Here’s how it can be done within an integration test:
using Microsoft.Extensions.DependencyInjection;
...
var scopeFactory = _factory.Server.Host.Services.GetService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
// Obtain in memory db context
var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
}


