I've been working on an implementation of a service, and have found that there are a number of operations where I need to read from a database to provide a caller with certain data or objects.
In-line with the .NET DI system, I have a DbContext injected into the classes that need to retrieve data. Something like the following:
public class RepositoryImpl
{
public RepositoryImpl(DbContext<Database> context)
{
_context = context;
}
// later
public DtoClass GetById(string id)
{
return _context.Entities.FirstOrDefault(entity => entity.Id == id);
}
}
However, to validate that the logic for retrieving the Entity by ID, I want to write automated tests. But from what I've read (for example: here), it looks like automated testing isn't really discussed for the integration point between application logic and data sources.
Is the only way to write effective automated tests for classes that inject the DbContext to provider in-memory data as a fake, then? That reads to me like an integration test unless you define a "unit" as both the DbContext and logic to interact with it, but that seems wrong to me.
What are the common practices or strategies for verifying this logic?