Lesson 16 of 93 · 5 min read · Updated
Readable Assertions with FluentAssertions 7: Should(), Collections, Exceptions and Licensing
On this page
FluentAssertions replaces Assert.Equal(expected, actual) with a chain that reads left to right: actual.Should().Be(expected). The benefit grows with the complexity of the check. Collection rules, exception details and object graphs become one readable statement with a precise failure message. This lesson rewrites familiar xUnit checks with FluentAssertions, explains a licensing change you need to know about, and shows where broad comparisons can hide a test's intent.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3 (with xunit.runner.visualstudio and Microsoft.NET.Test.Sdk)
- FluentAssertions 7.2.2 (Apache-2.0)
A Licensing Note First
FluentAssertions 8.x moved to a commercial licence. Using version 8 in a commercial product requires a paid licence. Version 7.x remains under the Apache-2.0 licence and is free to use, and that is the version pinned in this project:
Directory.Packages.props
Check this before you add the package, and check it again before you accept an automatic upgrade from a dependency bot. A routine "bump to 8.0" pull request changes your licence terms. If you want a free option that stays maintained, look at Shouldly, or AwesomeAssertions, a community fork of FluentAssertions that keeps the Apache-2.0 licence and a nearly identical API. The examples below work with 7.x.
The test project references the package without a version, because versions are managed centrally:
M02.XUnitBasics.Tests/M02.XUnitBasics.Tests.csproj
Values: Should().Be and Chained Conditions
M02.XUnitBasics.Tests/FluentAssertionsTests.cs
The subject comes first, then the expectation. You can no longer mix up the expected and actual arguments, which is an easy mistake with Assert.Equal. .And chains several conditions on the same subject. When one fails, the message includes the expression that was tested, for example "Expected product.Price to be 300M, but found 250M." That message comes from the code itself, so you rarely need a custom one. When you do want to explain the expectation, every assertion accepts an optional "because" text as its next argument, and FluentAssertions adds it to the failure message.
Collections in One Statement
M02.XUnitBasics.Tests/FluentAssertionsTests.cs
This test describes the whole contract of InStock(): two items, all in stock, sorted by name. With plain xUnit you would need Assert.Equal on the count, Assert.All for the stock rule and Assert.Collection or a manual comparison for the order. Here the products are added in reverse order on purpose, so the sort really has to happen. ContainInOrder then checks the SKUs in sequence.
Exceptions with Message Patterns
M02.XUnitBasics.Tests/FluentAssertionsTests.cs
The action is stored in a variable named act, which keeps the Arrange-Act-Assert shape visible. Should().Throw<T>() accepts T and derived types, like Assert.ThrowsAny in xUnit. Use ThrowExactly<T>() if the exact type matters. WithMessage takes a wildcard pattern, where * matches any text, so the test checks only the important part ("insufficient stock"). For async code, the matching call is await act.Should().ThrowAsync<T>(), and the same rule as in xUnit applies: forget the await and the test passes no matter what.
Structural Comparison and Its Pitfall
M02.XUnitBasics.Tests/FluentAssertionsTests.cs
BeEquivalentTo compares two objects member by member, recursively. It is the FluentAssertions counterpart of Assert.Equivalent, with more options: you can exclude members, compare only selected ones, or ignore collection order. When it fails, it lists every member that differs.
That power is also the risk. A broad BeEquivalentTo on a large DTO checks every field at once. The test then fails for changes nobody cares about, such as a new audit field, and the reader cannot tell which field the test is really about. Choose the scope on purpose. If the behaviour under test is "the subject line is set", assert the subject. Use full equivalence when the whole object really is the contract, such as a mapping from one model to another.
Finally, do not mix styles at random. Using xUnit assertions in one test and FluentAssertions in the next makes a codebase harder to read. Pick one style per project, or agree on a clear rule, such as FluentAssertions for collections and object graphs only.
Summary
- FluentAssertions reads subject first,
actual.Should().Be(expected), and its failure messages name the tested expression. - FluentAssertions 8.x is commercial. Version 7.x (Apache-2.0) is free, and Shouldly and AwesomeAssertions are free alternatives.
- Chain collection rules with
.And: count, element rules and order in one statement. Should().Throw<T>()accepts derived types, andWithMessagetakes wildcard patterns. AlwaysawaitThrowAsync.- Use
BeEquivalentTodeliberately. A comparison that is broader than the contract hides the test's intent.