Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 5 of 8

FluentAssertions - Readable Test Assertions

FluentAssertions replaces `Assert.Equal(a, b)` with `a.Should().Be(b)` - natural English readable assertions that produce detailed failure messages. For complex objects, it shows exactly which property differed.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

FluentAssertions Patterns

FluentAssertions Patterns
using FluentAssertions;

// ━━ Basic assertions ━━
value.Should().Be(expected);
value.Should().NotBe(unexpected);
value.Should().BeNull();
value.Should().NotBeNull();
value.Should().BeTrue();
value.Should().BeFalse();

// ━━ Numeric ━━
price.Should().BeGreaterThan(0);
price.Should().BeLessThanOrEqualTo(999.99m);
price.Should().BeInRange(1m, 999m);
price.Should().BeApproximately(9.99m, precision: 0.01m);

// ━━ String ━━
slug.Should().Be("hello-world");
slug.Should().StartWith("hello");
slug.Should().EndWith("world");
slug.Should().Contain("llo");
slug.Should().NotContain("#");
slug.Should().MatchRegex(@"^[a-z0-9-]+$");
slug.Should().HaveLength(11);

// ━━ Collections ━━
products.Should().HaveCount(3);
products.Should().NotBeEmpty();
products.Should().BeEmpty();
products.Should().Contain(p => p.Name == "Widget");
products.Should().AllSatisfy(p => p.Price.Should().BeGreaterThan(0));
products.Should().BeInAscendingOrder(p => p.Name);
products.Should().HaveCountGreaterThan(0);
products.Should().ContainSingle(p => p.IsActive);

// ━━ Object property comparison ━━
product.Should().BeEquivalentTo(expected, opts =>
    opts.Excluding(p => p.CreatedAt)  // ignore timestamp (set by DB)
        .Excluding(p => p.UpdatedAt));

// ━━ Exception assertions ━━
await action.Should().ThrowAsync<NotFoundException>();
await action.Should().ThrowAsync<ValidationException>()
    .WithMessage("*Name*required*");
await action.Should().NotThrowAsync();

// ━━ HTTP response assertions ━━
response.StatusCode.Should().Be(System.Net.HttpStatusCode.Created);
response.Headers.Location.Should().NotBeNull();
response.Headers.Location!.ToString().Should().StartWith("/api/products/");

// ━━ Result<T> custom assertions ━━
var result = await service.CreateAsync(request, ct);
result.IsSuccess.Should().BeTrue(because: "valid request should succeed");
result.Value.Should().NotBeNull();
result.Value!.Name.Should().Be("Widget");
result.Error.Should().BeNull();

// Placeholder types
decimal price = 0, value = 0;
string slug = "", expected = "";
System.Collections.Generic.List<dynamic> products = [];
dynamic product = new { }, action = (Func<Task>)(() => Task.CompletedTask), response = new System.Net.Http.HttpResponseMessage();

Tip

Tip

Practice FluentAssertions Readable Test Assertions in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of FluentAssertions Readable Test Assertions from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with FluentAssertions Readable Test Assertions is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/8 done