Making fluent assertions in unit testing
FluentAssertions are a set of extension methods that make writing and reading the code in unit tests and the error messages of failing tests more similar to a natural human language like English.
It works with most unit testing frameworks, including xUnit. When you add a package reference for a test framework, FluentAssertions will automatically find the package and use it for throwing exceptions.
After importing the FluentAssertions
namespace, call the Should()
extension method on a variable and then one of the hundreds of other extension methods to make assertions in a human-readable way. You can chain multiple assertions using the And()
extension method or have separate statements, each calling Should()
.
Making assertions about strings
Let’s start by making assertions about a single string
value:
- Use your preferred code editor to add a new xUnit Test Project /
xunit
namedFluentTests
to aChapter06
solution...