
Testing exceptions in your code is an important part of ensuring that your code is robust and behaves correctly under various scenarios. One way to test exceptions in .NET is by using the FluentAssertions lib.
Prerequisites
Before we get started, you’ll need to install the FluentAssertions library. You can do this via NuGet by running the following command:
1Install-Package FluentAssertions
2# or
3dotnet add package FluentAssertions
To use FluentAssetions you have to add the following using statement to your code:
1using FluentAssertions;
Testing for Exceptions
The most basic way to test for exceptions with FluentAssertions is to use the Should().Throw() method. This method allows you to specify the type of exception that you expect to be thrown and it will fail the test if the exception is not thrown or if a different exception is thrown.
Here’s an example of how to use Should().Throw() to test for exceptions:
1
2[Test] // or [Fact] if you're using XUnit
3public void TestMethod_ShouldThrowException()
4{
5 // arrange
6 SomeObject someObject = new();
7
8 // act
9 Action action = () => someObject.SomeMethod();
10
11 // assert
12 action.Should().Throw<SomeException>();
13}
In this example, we’re testing that the SomeMethod() method of the someObject object throws a SomeException. If the exception is not thrown or if a different exception is thrown, the test will fail.
Testing for Specific Exception Messages
In addition to testing for the type of exception that is thrown, you can also test for the specific message of the exception. This is useful if you want to ensure that the exception message is correct and provides helpful information to the user.
To test for a specific exception message, you can use the WithMessage() method in combination with Should().Throw().
1[Test] // or [Fact] if you're using XUnit
2public void TestMethod_ShouldThrowExceptionWithSpecificMessage()
3{
4 // arrange
5 SomeObject someObject = new();
6
7 // act
8 Action action = () => someObject.SomeMethod();
9
10 // assert
11 action.Should().Throw<SomeException>()
12 .WithMessage("This is the expected exception message.");
13}
In this example, we’re testing that the SomeMethod() method throws a SomeException with the message “This is the expected exception message.” If the exception message is different or if a different exception is thrown, the test will fail.
FluentAssertions is a great library for testing exceptions in .NET. It provides a simple and easy to use API for testing exceptions and it can help you write robust and reliable code.
FluentAssertions on GitHub: https://github.com/fluentassertions/fluentassertions
Related articles

Oct 05, 2026 - 12 min read
Problem Details in ASP.NET Core and .NET 11
builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Sep 21, 2026 - 11 min read
Resilience in .NET with Microsoft.Extensions.Resilience
Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …

Sep 14, 2026 - 10 min read
What Matters in ASP.NET Core 11
ASP.NET Core 11 has no single feature that fundamentally changes how web applications are built. That is not a criticism. Mature frameworks …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
