Given a method that throws an Exception
static void MethodThatThrows() =>
throw new("The Message");That exception behavior can be verified using Verify.Throws:
[Fact]
public Task TestMethodThatThrows() =>
Throws(MethodThatThrows);Resulting in the following snapshot file:
{
Type: Exception,
Message: The Message,
StackTrace: at ThrowsTests.MethodThatThrows()
}Often the exception stack trace can be noisy and fragile to causing false failed tests. To exclude it use IgnoreStackTrace:
[Fact]
public Task TestMethodThatThrowsIgnoreStackTraceFluent() =>
Throws(MethodThatThrows)
.IgnoreStackTrace();[Fact]
public Task TestMethodThatThrowsIgnoreStackTraceSettings()
{
var settings = new VerifySettings();
settings.IgnoreStackTrace();
return Throws(MethodThatThrows, settings);
}VerifierSettings.IgnoreStackTrace();{
Type: Exception,
Message: The Message
}There are specific named Throws* method for methods that return a Task or a ValueTask.
static async Task MethodThatThrowsTask()
{
await Task.Delay(1);
throw new("The Message");
}[Fact]
public Task TestMethodThatThrowsTask() =>
ThrowsTask(MethodThatThrowsTask);static async ValueTask MethodThatThrowsValueTask()
{
await Task.Delay(1);
throw new("The Message");
}[Fact]
public Task TestMethodThatThrowsValueTask() =>
ThrowsValueTask(MethodThatThrowsValueTask);