C# – How to match an anonymous type parameter in a mocked method

When an anonymous type is defined in one assembly, it won’t match an anonymous type defined in another assembly. This causes problems when you’re unit testing and trying to mock a method that has an anonymous type parameter. For example, let’s say you’re trying to unit test the following method: To unit test this, you … Read more

C# – Unit testing code that does File IO

If your code does File IO, such as reading text from a file, then it’s dependent on the file system. This is an external dependency. In order to make the unit tests fast and reliable, you can mock out the external dependencies. To mock out the file system dependency, you can wrap the File IO … Read more

Moq – Verifying parameters passed to a mocked method

When you need to verify that the code under test called a method with the expected parameters, you can mock the method with Moq and use Verify() + It.Is<T>() to check the parameters passed in. Verify() asserts that the method call happened as expected with the specified parameters. Here’s an example. This is verifying that … Read more

Moq – Capture parameters with Callback()

When you’re using Moq to set up a mocked method, you can use Callback() to capture the parameters passed into the mocked method: There are two main use cases for capturing parameters in a test: In this article, I’ll show examples of using Callback() in those two scenarios, and then I’ll explain some problems to … Read more

Error CS0854 when you’re using Moq with optional parameters

Problem Optional parameters aren’t optional when you’re using Moq. When you’re setting up or verifying a method call on a mock object, and the method has an optional parameter that you didn’t specify a value for, you’ll get the following compiler error: Error CS0854 – An expression tree may not contain a call or invocation … Read more

C# – How to unit test console output

There’s two ways to unit test code that writes to the console (Console.WriteLine() / Console.Write()): In this article, I’ll show how to do both options. Option 1 – Capture the output with Console.SetOut() Let’s say you want to unit test the following code that outputs to the console with Console.WriteLine(): You can unit test this … 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# – How to unit test code that uses Dapper

Dapper makes your code difficult to unit test. The problem is that Dapper uses static extension methods, and static methods are difficult to mock out. One approach is to wrap the Dapper static methods in a class, extract out an interface for that wrapper class, and then dependency inject the wrapper interface. In the unit … 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

Moq – Return different values with SetupSequence

When you’re mocking a method that’s called multiple times, you may want to change the behavior of the method each time it’s called. The way you do this with Moq is by using SetupSequence(), like this: Note: You can also make it throw an exception in the sequence. Example of code I want to test … Read more

C# – How to unit test async methods

Let’s say you have the following async method you want to test: Here’s how to unit test this: This is awaiting the method you’re testing. To await it, you must make the unit test method return async Task. This example is a bit simplistic. In the real world when you are working with async methods, … Read more

How to mock static methods

The need to mock static methods in order to add a unit test is a very common problem. It’s often the case that these static methods are in third-party libraries. There are many utility libraries that are completely made up of static methods. While this makes them very easy to use, it makes them really … Read more