Search Results for

    Show / Hide Table of Contents

    EmptyCollection Constraint

    EmptyCollectionConstraint tests that an IEnumerable contains no items. An ArgumentException is thrown if the actual value is not an IEnumerable or is null.

    Usage

    Is.Empty
    Is.Not.Empty
    

    Examples

    [Test]
    public void EmptyCollectionConstraint_Examples()
    {
        // Empty collections
        Assert.That(new int[] { }, Is.Empty);
        Assert.That(new List<string>(), Is.Empty);
        Assert.That(Array.Empty<int>(), Is.Empty);
        Assert.That(Enumerable.Empty<string>(), Is.Empty);
    
        // Non-empty collections
        Assert.That(new[] { 1, 2, 3 }, Is.Not.Empty);
        Assert.That(new List<string> { "item" }, Is.Not.Empty);
    }
    

    Notes

    1. Is.Empty is a polymorphic constraint that works with strings, collections, and directories.
    2. When applied to an IEnumerable, it creates an EmptyCollectionConstraint internally.
    3. Passing null throws an ArgumentException. Use Is.Null.Or.Empty pattern if null is acceptable.

    See Also

    • Empty Constraint - Polymorphic empty constraint
    • EmptyString Constraint - For string-specific empty tests
    • ExactCount Constraint - Test for specific item count
    • 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