Cleaner unit test assertions in C# with Fluent Assertions

Published on in C# and Elsewhere

The Fluent Assertions package makes unit test assertions read like English and also improves the error messages.

Watching Nick Chapsas's video on the Fluent Assertions package made me want to start using Fluent Assertions at work.

Example from the video (_sut = system under test):

// Before
Assert.Equal(expected, result);
Assert.StartsWith("The result is: ", _sut.Text);
Assert.EndsWith(result.ToString(), _sut.Text);

// After
result.Should().Be(expected);
_sut.Text.Should().StartWith("The result is: ");
_sut.Text.Should().EndWith(result.ToString());

So much better. Reads like English.

Another benefit is that the messages from failed assertions are more detailed.