Fluent Assertions

FluentAssertions provides extension methods for your asserts

Basic Assertions

1
2
3
object theObject = null;
theObject.Should().BeNull("because the value is null");
theObject.Should().NotBeNull();

Equivalent To

Should().BeEquivalentTo(...) asserts that an object is equivalent to another object.

1
2
3
4
5
// v5.9.0
actual.Should().BeEquivalentTo(expectedModel);

// v2.2.0
objectA.ShouldBeEquivalentTo(objectB);

Awaiting Exceptions

“Invokes the specified action on a subject so that you can chain it with any of the assertions from FluentAssertions.Specialized.GenericAsyncFunctionAssertions`”

// Act
// Assert
classUnderTest
    .Awaiting(x => x.SomeMethod(someParameter))
    .Should().Throw<FooCustomExceptionException>()
    .And.StatusCode.Should().Be(StatusCodes.Status503ServiceUnavailable); // Microsoft.AspNetCore.Http

References