Search Results for

    Show / Hide Table of Contents

    Throws Constraint

    ThrowsConstraint is used to test that code, represented as a delegate, throws a particular exception. It may be used alone to test the exception type, or with additional constraints applied to the exception itself. The related ThrowsNothingConstraint asserts that the delegate does not throw.

    Usage

    Throws.Exception
    Throws.TypeOf<T>()
    Throws.TypeOf(Type expectedType)
    Throws.InstanceOf<T>()
    Throws.InstanceOf(Type expectedType)
    
    // Common exception shortcuts
    Throws.ArgumentException
    Throws.ArgumentNullException
    Throws.InvalidOperationException
    Throws.TargetInvocationException
    
    // For testing nothing is thrown
    Throws.Nothing
    

    Modifiers

    .With.Message.EqualTo(string)     // Test exception message
    .With.Message.Contains(string)    // Test message contains substring
    .With.Property("Name").EqualTo(x) // Test exception property
    .With.InnerException.TypeOf<T>()  // Test inner exception
    

    Examples

    [Test]
    public void ThrowsConstraint_Basic_Examples()
    {
        // Test for specific exception type
        Assert.That(() => ThrowArgumentException(), Throws.TypeOf<ArgumentException>());
        Assert.That(() => ThrowArgumentException(), Throws.InstanceOf<Exception>());
    
        // Test exception message
        Assert.That(() => throw new ArgumentException("bad value"),
            Throws.ArgumentException.With.Message.EqualTo("bad value"));
    
        // Test with lambda expression
        Assert.That(() => int.Parse("abc"), Throws.TypeOf<FormatException>());
    
        // Assert no exception is thrown
        Assert.That(() => SafeMethod(), Throws.Nothing);
    }
    
    private static void ThrowArgumentException() => throw new ArgumentException("test");
    private static void SafeMethod() { }
    

    Additional Examples

    [Test]
    public void ThrowsConstraint_Examples()
    {
        Assert.That(() => throw new ArgumentException(), Throws.ArgumentException);
        Assert.That(() => throw new InvalidOperationException("test"),
                   Throws.InvalidOperationException.With.Message.EqualTo("test"));
        Assert.That(() => { }, Throws.Nothing);
    }
    

    Notes

    1. Throws.TypeOf requires an exact type match. Use Throws.InstanceOf to allow derived exception types.
    2. Throws.Exception can be followed by additional constraints on the exception object. Avoid using it alone without type checking, as you should generally know what exception to expect.
    3. For async code, use Assert.ThrowsAsync or test the async delegate directly with Throws.
    4. Throws.InnerException tests the InnerException property. Combine it with outer exception type tests for full validation.

    See Also

    • ThrowsNothing Constraint
    • Assert.Throws
    • Assert.ThrowsAsync
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0