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
Has.Noneis the inverse ofHas.Some- the constraint passes if no items match.- The constraint fails as soon as the first matching item is found.
- An empty collection satisfies
Has.Nonefor any constraint.
See Also
- AllItems Constraint - All items must match
- SomeItems Constraint - At least one item matches
- ExactCount Constraint - Specific number of items match