Unit testing with AutoFixture, NSubstitute, and FluentAssertions
xUnit is a powerful testing framework, but it has some limitations. xUnit, by itself, lacks a concise assertion syntax, built-in dependency mocking, and the ability to generate random test data. In this recipe, we will learn how to automatically mock our dependencies with NSubstitute
. We will also make our tests much more robust by automatically creating test data to assert against using AutoFixture
. Automatic test data makes our tests more robust; the data pushed through our system is not only limited to what we happen to think up. Finally, we will write more expressive assertions using the FluentAssertions
library.
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/UnitTests2.
You can also just pick up from where the preceding recipe left off.
How to do it…
- Open
BookServiceTests.cs
. Copy...