Organizing unit tests with a base service class
We can better organize our tests by creating an abstract class. This abstract class establishes a framework for mocking our repositories, services, or anything else we need to share among tests. This pattern can work well with either a test fixture or a series of independent tests.
Getting ready
You can download the starter project here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter07/UnitTests3.
You can also continue from the preceding recipe.
How to do it…
- Create a new file called
BookServiceTestsBase.cs. Import the namespaces for our API services and repositories as well as theNSubstitutelibrary:using Books.Repositories; using Books.Services; using NSubstitute; namespace Tests.Services;
- Implement protected properties to hold
ServiceandRepository:public abstract class BooksServiceTestsBase { protected IBooksRepository Repository { get;...