Writing Custom Assertions
The anatomy of an assertion
person.Manager
should be awesome
"an awesome person"
but was
nullThe basic pattern
public static class CustomAssertions
{
public static void ShouldBeAwesome(
this Person? actual,
string? customMessage = null,
[CallerArgumentExpression(nameof(actual))] string? actualExpression = null)
{
if (actual is not { IsAwesome: true })
throw new ShouldAssertException(
new ExpectedActualShouldlyMessage("an awesome person", actual,
customMessage, actualExpression: actualExpression).ToString());
}
}Class
Message shape
Composing existing assertions
Predicate-style assertions with AssertAwesomely
Verifying your wiring with the trip-wire
Wrapping ShouldMatchApproved
Polish
Do I still need [ShouldlyMethods]?
Last updated