Search Results for

    Show / Hide Table of Contents

    NoItem Constraint

    NoItemConstraint applies a constraint to each item in an IEnumerable, succeeding only if no items satisfy the constraint. An exception is thrown if the actual value does not implement IEnumerable.

    Usage

    Has.None.<constraint>
    

    Examples

    [Test]
    public void NoItemConstraint_Examples()
    {
        int[] numbers = { 1, 2, 3, 4, 5 };
        string[] names = { "Alice", "Bob", "Carol" };
    
        // No items should satisfy the constraint
        Assert.That(numbers, Has.None.LessThan(0));         // No negatives
        Assert.That(numbers, Has.None.GreaterThan(100));    // None > 100
        Assert.That(names, Has.None.Null);                  // No nulls
        Assert.That(names, Has.None.Empty);                 // No empty strings
        Assert.That(names, Has.None.EqualTo("Dave"));       // "Dave" not in list
    
        // Property-based constraints
        Assert.That(names, Has.None.Length.GreaterThan(10)); // No name > 10 chars
    
        // Multiple conditions
        Assert.That(numbers, Has.None.LessThanOrEqualTo(0));
    }
    

    Notes

    1. Has.None is the inverse of Has.Some - the constraint passes if no items match.
    2. The constraint fails as soon as the first matching item is found.
    3. An empty collection satisfies Has.None for any constraint.

    See Also

    • AllItems Constraint - All items must match
    • SomeItems Constraint - At least one item matches
    • ExactCount Constraint - Specific number of items match
    • 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