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# – How to unit test a model validation attribute

You can unit test a validation attribute by creating an instance of it and then testing the two methods: In this article, I’ll show examples of unit testing these methods in a custom validation attribute and in a built-in validation attribute (i.e. [Range]). Unit testing a custom validation attribute Consider the following custom validation attribute … Read more

ASP.NET Core – How to unit test your middleware class

There are three requirements for unit testing a middleware class: Here’s an example: This is a simple test that only checks the response status code. By passing in DefaultHttpContext, you have control over the request and response objects. You can set the request to whatever you need, and then verify the response. I’ll show examples … Read more

C# – Use FluentAssertions to improve unit tests

FluentAssertions is a library that improves unit tests by providing better failure messages, simplifies assertions in many scenarios, and provides a fluent interface (which improves code readability). In this article, I’ll show a few examples of how FluentAssertions can improve unit tests by comparing it with the built-in assertions (from Microsoft.VisualStudio.TestTools.UnitTesting). Install FluentAssertions Add the … Read more

C# – Parameterized tests in xUnit

Here’s an example of adding a parameterized unit test in xUnit: To parameterize a unit test, you have to do three things: If you’re used to doing parameterized tests with MSUnit, [Theory] is equivalent [DataMethod], and [InlineData] is equivalent to [DataRow]. In the rest of the article, I will show how to add parameterized tests … Read more

ASP.NET Core – How to unit test an ApiController

The key to unit testing an ApiController class is to mock out all of its dependencies, including the controller’s HttpContext property, like this: If the controller method you’re testing uses anything from the HttpContext, then you’ll want to swap in your own value. Otherwise HttpContext will be null and you’ll get a NullReferenceException. Fortunately Microsoft … Read more

C# – Using ClassInitialize in a test class

You can use the ClassInitialize attribute on a method when you want to initialize something for all unit tests in a test class. This initialization method only runs once and is ran before any of the unit tests. Here’s an example of how to add ClassInitialize: Note: ClassCleanup is the opposite of ClassInitialize. It runs … Read more

C# – Use StringAssert when testing a string for substrings

When you’re testing if two strings are equal, you can simply use Assert.AreEqual(). When you’re testing if a string contains a substring or a pattern, typically developers use Assert.IsTrue() with a substring method or regex. You should use StringAssert instead, because it gives better failure messages. Note: StringAssert is nice because it’s a built-in class. … Read more

C# – How to use Assert.ThrowsException

Use Assert.ThrowsException<T>() in a unit test to verify that the code throws a specific type of exception. Here’s an example of asserting that ArgumentNullException is thrown: You specify the type of exception you’re expecting and then pass in a lambda that calls the code you’re testing. The assertion succeeds if and only if the code … Read more

C# – Can’t pass decimal parameter in DataTestMethod

I have a parameterized unit test with decimal parameters. When I run the test, I get the following exception: System.ArgumentException: Object of type ‘System.Double’ cannot be converted to type ‘System.Decimal’. Solution Change the parameters to doubles and convert them to decimals inside the test method. Why is it throwing an exception? You have to pass … Read more