Posts Unit Testing .NET 5 Console Applications with Dependency Injection
Unit Testing .NET 5 Console Applications with Dependency Injection
Cancel

Unit Testing .NET 5 Console Applications with Dependency Injection

In my last post, I created a .NET 5 console application and configured dependency injection. This was pretty straight-forward and only needed a couple of lines of code.

Today, I will show you how to create unit tests when using dependency injection in a .NET 5 (or .NET Core) console application.

Create Unit Tests using xUnit

You can find the code of the demo on Github.

Create a new .NET 5 test project using xUnit and create a reference to the main project. You can use any unit testing and faking framework you like. For this demo, I am using xUnit and Moq. Additionally, I am using FluentAssertions to make the assertions more readable.

Implementing unit tests for a .NET 5 console application with dependency injection is the same as for any other project. The only minor difficulty is how to test the following method which creates an instance of IGreeter and then calls the Greet() method on it.

The difficulty when testing this method is the IServiceProvider which has to be configured to be able to create an instance of the IGreeter interface.

Create a fake Service Provider

The solution to the problem is to create a fake IServiceProvider and IServiceScope object. The IServiceProvider fake then can be configured to return a fake IServiceScopeFactory object. This fake object can be used as the IServiceProvider parameter of the GreetWithDependencyInjection() method. The full code looks as follows:

The code in the constructor might look complicated but if you start a new project, all you have to do is to copy it into the new project and you are good to go.

Test Classes without the IServiceProvider Interface

As previously mentioned, every other class besides the Program.cs can be tested as you are used to. For example, testing the ConsoleGreeter is straight-forward. Create a new object of the ConsoleGreeter, add a fake interface in the constructor and then call the method you want to test. The method should return the value you expect.

Testing the Unit Tests

Run all the unit tests and you should see both running successfully.

The Tests ran successfully

The Tests ran successfully

Conclusion

Creating unit tests for a .NET 5 console application that uses dependency injection only takes a couple of lines of code to configure the service provider. This code can be copied to any new project, making it even easier to set up.

You can find the code of the demo on Github.

This post is licensed under CC BY 4.0 by the author.