Component or service testing is a method where we bypass the UI and directly test the API (in our case, the ASP.NET Core Web API). Using this test, we confirm that an individual service does not have any code bugs and that it is working fine functionality-wise.
Testing a service does not mean it is an independent service. This service might be interacting with an external service. In such a scenario, we should not call the actual service but use the mock and stub approach. The reason for this is our motto: to test code and make sure it is bug-free. In our case, we will use the moq framework for mocking our services.
There are a few things worth noting for component or service testing:
- As we need to verify the functionality of the services, these kinds of tests should be small and fast
- With the help of mocking, we don't need to deal with the actual database; therefore, test execution time is less or nominally higher
- The scope of these tests is broader than unit tests