I have this piece of code I wrote and is used on an endpoint. Basically, a SearchCatalogItemRequest
is just an object that contains just a string
which is then used to filter a search using EF Core 7.
public async Task<List<Drug>> SearchDrugs(SearchCatalogItemRequest search)
{
var drugs = _unitOfWork.DrugRepository.Get()
.Where(x => x.Name.ToLower().Contains(search.SearchString.Trim().ToLower())).ToList();
return drugs;
}
I've done Unit Tests before, but it has been for more 'fleshy' pieces of codes; this one just returns a list from the database based on a filter. What could I test in this case? Currently no test cases come to mind.
CodePudding user response:
A persistence unit test is appropriate for this piece of code. The test can setup test data in the database that contains multiple rows, some of which meet the search criteria. The test would invoke the method to retrieve rows and then assert that only the rows that should have been returned based on the search criteria are returned. The test would subsequently clean up the database in the Dispose method.