
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

Dec 14, 2022 - 3 min read
Get Process Id by Name or Path with .NET
Getting the process ID by the name or path of a process can be useful for a variety of reasons, such as being able to kill a hanging process …

Dec 08, 2022 - 2 min read
Avoid Caching with MemoryCache and GetOrCreate in .NET
GetOrCreate or its asynchronous counterpart GetOrCreateAsync is an extension method to create a cache entry if it does not exist yet.

Nov 20, 2022 - 3 min read
Validate Configuration Options with ASP.NET Core
Configuring an ASP.NET Core application can be done in a variety of ways, including using command-line arguments, environment variables and …
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.
