ThrowsNothing Constraint
ThrowsNothingConstraint tests that the code under test does not throw any exception.
Usage
Throws.Nothing
Examples
[Test]
public void ThrowsNothingConstraint_Examples()
{
// Test that method completes without throwing
Assert.That(() => Add(1, 2), Throws.Nothing);
// Verify cleanup doesn't throw
var resource = new DisposableResource();
Assert.That(() => resource.Dispose(), Throws.Nothing);
}
private static int Add(int a, int b) => a + b;
private class DisposableResource : IDisposable
{
public void Dispose() { }
}
Notes
- Use
Throws.Nothingto explicitly verify that code doesn't throw. - This is different from not testing at all - it documents the expectation.
- If the delegate throws any exception, the constraint fails with the exception details.
See Also
- Throws Constraint - Test that code throws specific exceptions