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
Is.Emptyis a polymorphic constraint that works with strings, collections, and directories.- When applied to an
IEnumerable, it creates anEmptyCollectionConstraintinternally. - Passing
nullthrows anArgumentException. UseIs.Null.Or.Emptypattern 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