Challenge: Improving Code Coverage Metrics

Test yourself by improving test coverage.

In the playground for this challenge, we have a Web API application that retrieves user data from an external service. If the service is unreachable or encounters any other problem, the API endpoints still return a status code indicating success. However, in case of an error, the response code is 204 No Content.

Currently, we’ve run some unit tests against the business logic layer of the application. However, if we execute the tests, we see that the code coverage is very low. This is due to the following:

  • We only execute the tests against the business layer, ignoring the application start-up code and API endpoints.

  • We mock the service that makes external HTTP requests.

The goal of this challenge is to increase the code coverage by adding some integration tests. We already have a placeholder file for the tests, called ApiTests.cs. We’ll need to add tests for both GET endpoints in the UserController.cs file of the WebApiApp project, and those tests should assert that the status code returned from those endpoints indicates success. This can be achieved by checking the value of the boolean IsSuccessStatusCode property of the response object if we make our requests via a HTTP client.

Get hands-on with 1200+ tech skills courses.