Controller unit tests with custom fixtures
In this recipe, we will set up unit testing on the Controller
layer. We will explore the expanded version of the FluentAssertions
library, FluentAssertions.AspNetCore.Mvc
, which will allow us to assert against HTTP results and not only internal method logic. We will provide dependencies to the controller via AutoNSubstitute
. Also, we will create a custom fixture to provide a consistent environment for our controller tests.
Getting ready
The starter project for this recipe can be found here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter07/UnitTestsController.
You can also continue from the preceding recipe.
How to do it…
- First, let’s import the expanded
FluentAssertions
package for testing the controller’s action methods. Make sure you add this only in theUnit.Tests
project.
Important note
FluentAssertions.AspNetCore.Mvc
cannot coexist with the standard...