
This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.
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 FluentAssertionsTo use FluentAssetions you have to add the following using statement to your code:
using 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

- 16 min read
Production Rate Limiting in ASP.NET Core
Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

- 11 min read
The Big Decimal Bugfix with .NET 11
For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

- 10 min read
Distroless, Chiseled or Alpine: Choosing a .NET Base Image
Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …
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.
