Entity Framework Core Interview Question #176
How should EF Core code be tested?
Testing, Production Practices & Senior Real-World Scenarios Senior Advanced
Quick Interview Answer
Test business behavior at the level where it matters. Pure domain logic should usually be tested without EF Core, while persistence behavior such as mappings, constraints, transactions, query translation, and concurrency requires tests that exercise a real relational provider. Keep tests deterministic and give each test isolated data.
Detailed Explanation
Test business behavior at the level where it matters. Pure domain logic should usually be tested without EF Core, while persistence behavior such as mappings, constraints, transactions, query translation, and concurrency requires tests that exercise a real relational provider. Keep tests deterministic and give each test isolated data.
Code Example
await using var db = CreateTestDbContext();
db.Products.Add(new Product { Name = "Test", Price = 10 });
await db.SaveChangesAsync();
Assert.Equal(1, await db.Products.CountAsync());