ASP.NET Core Web API Interview Question #156
Should Web API tests use an in-memory database?
Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced
Quick Interview Answer
An in-memory database can make some tests fast, but it may not reproduce the behavior of the production database provider. Differences in SQL translation, constraints, transactions, collation, and provider-specific features can hide real defects.
Detailed Explanation
An in-memory database can make some tests fast, but it may not reproduce the behavior of the production database provider. Differences in SQL translation, constraints, transactions, collation, and provider-specific features can hide real defects.
For data-access behavior that depends on relational semantics, a disposable test database using the actual provider or a close equivalent gives stronger confidence.
Code Example
// Test registration can point EF Core to a dedicated
// disposable SQL database or test container.
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(testConnectionString));