ASP.NET Core – How to unit test a custom InputFormatter

In this article, I’ll show how to unit test a custom InputFormatter. The main thing to test is the output of the ReadRequestBodyAsync() method. To test this, you have to pass in an InputFormatterContext object containing the request body. As an example, I’ll show how to unit test the following ReadRequestBodyAync() method: Note: This parses … Read more

C# – Convert a list of strings into a set of enums

Let’s say you have a list of HTTP status codes that you read in when the service starts up (perhaps from appsettings.json or from the database). Whenever you send an HTTP request, you want to check if the returned status code is in this list of status code. To make things more efficient, you want … Read more

C# – How to unit test code that uses HttpClient

When you want to unit test code that uses HttpClient, you’ll want to treat HttpClient like any other dependency: pass it into the code (aka dependency injection) and then mock it out in the unit tests. There are two approaches to mocking it out: In this article I’ll show examples of these two approaches. Untested … Read more