logikfabrik

xUnit assert and ex­cep­tions

1 min read

To test for a specific type of exception, that should be thrown:

await Assert.ThrowsAsync<Exception>(() => sut.Foo());

Or:

var e = await Record.ExceptionAsync(async () => await sut.Foo());

Assert.IsType<Exception>(e);

And to test for an exception, that shouldn’t be thrown:

var e = await Record.ExceptionAsync(async () => await sut.Foo());

Assert.Null(e);