Skip to content

ExactCountConstraint

Chris Maddock edited this page Jul 11, 2017 · 8 revisions

ExactCountConstraint has two functions.

In its simplest use, it simply verifies the number of items in an array, collection or IEnumerable, providing a way to count items that is independent of any Length or Count property.

When used with another constraint, it applies that constraint to each item in the array, collection or IEnumerable, succeeding if the specified number of items succeed.

An exception is thrown if the actual value passed does not implement IEnumerable.

Constructor

ExactCountConstraint(int expectedCount)
ExactCountConstraint(int expectedCount, Constraint itemConstraint)

Syntax

Has.Exactly(int expectedCount)...

Examples of Use

int[] array = new int[] { 1, 2, 3 };

Assert.That(array, Has.Exactly(3).Items);
Assert.That(array, Has.Exactly(2).Items.GreaterThan(1));
Assert.That(array, Has.Exactly(3).LessThan(100));
Assert.That(array, Has.Exactly(2).Items.EqualTo(1).Or.EqualTo(3));
Assert.That(array, Has.Exactly(1).EqualTo(1).And.Exactly(1).EqualTo(3));

Note: The keyword Items is optional when used before a constraint but required when merely counting items with no constraint specified.

Clone this wiki locally