Learn how to write testable code in C# in only 15 minutes

Ron Jonkers
6 min readJan 16, 2021

In this course I will teach you how to write code that is easy to test and how to create the tests themselves. And even better: it will only take you 15 minutes!

Why you should want to unit test

As an application grows, it becomes harder and harder to do manual testing. At some point the effort that goes into it simply becomes too much. The solution to this problem is automated testing. There are various types of automated tests, but in this course we will focus on unit tests.

In most cases unit tests are written by developers. Unit tests are called white box tests, because the developer knows the internal working of the application. The main benefit of unit tests is that they are very fast.

Tightly coupled code is not suited for unit testing

One thing that is essential to unit testing is that the code must be written in a way that allows it to be tested. I will start off by showing an example of a class that was not written with testability in mind:

public class ApiMonitoringRepository
{
private static readonly HttpClient _httpClient = new HttpClient();
public async Task<HealthStatus> IsHealthyAsync()
{
var response =…

--

--