Collection constraints perform tests that are specific to collections. The following collection constraints are provided. Before NUnit 2.4.6, these constraints only operated on true Collections. Beginning with 2.4.6, they can work with any object that implements IEnumerable.
Beginning with NUnit 2.4.2, use of an improper argument type caused tests to fail. Later releases give an error rather than a failure, so that negated constraints will not appear to succeed.
For example, both of the following statements give an error in later releases, but the second would have succeeded in earlier versions of NUnit.
int[] iarray = new int[] { 1, 2, 3 }; Assert.That( 5, Is.SubsetOf( iarray ) ); // Fails in early releases Assert.That( 5, Is.Not.SubsetOf( iarray ) ); /
Applies a constraint to each item in a collection, succeeding only if all of them succeed.
AllItemsConstraint(Constraint itemConstraint)
Is.All... Has.All...
int[] iarray = new int[] { 1, 2, 3 }; string[] sarray = new string[] { "a", "b", "c" }; Assert.That( iarray, Is.All.Not.Null ); Assert.That( sarray, Is.All.InstanceOf() ); Assert.That( iarray, Is.All.GreaterThan(0) ); Assert.That( iarray, Has.All.GreaterThan(0) );
Applies a constraint to each item in a collection, succeeding if at least one of them succeeds.
SomeItemsConstraint(Constraint itemConstraint)
Has.Some...
int[] iarray = new int[] { 1, 2, 3 }; string[] sarray = new string[] { "a", "b", "c" }; Assert.That( iarray, Has.Some.GreaterThan(2) ); Assert.That( sarray, Has.Some.Length(1) );
Applies a constraint to each item in a collection, succeeding if the specified number of items succeed.
ExactCountConstraint(int expectedCount, Constraint itemConstraint)
Has.Exactly(int expectedCount)...
int[] array = new int[] { 1, 2, 3 }; Assert.That( array, Has.Exactly(1).EqualTo(3) ); Assert.That( array, Has.Exactly(2).GreaterThan(1) ); Assert.That( array, Has.Exactly(3).LessThan(100) );
Applies a constraint to each item in a collection, succeeding only if all of them fail.
NoItemConstraint(Constraint itemConstraint)
Has.None...
int[] iarray = new int[] { 1, 2, 3 }; string[] sarray = new string[] { "a", "b", "c" }; Assert.That( iarray, Has.None.Null ); Assert.That( sarray, Has.None.EqualTo("d") ); Assert.That( iarray, Has.None.LessThan(0) );
Tests that a collection contains only unique items.
UniqueItemsConstraint()
Is.Unique
string[] sarray = new string[] { "a", "b", "c" }; Assert.That( sarray, Is.Unique );
Tests that a collection contains an object.
CollectionContainsConstraint( object )
Has.Member( object ) Contains.Item( object )
...Using(IComparer comparer) ...Using(IComparer<T> comparer) ...Using (Comparison<T> comparer)
int[] iarray = new int[] { 1, 2, 3 }; string[] sarray = new string[] { "a", "b", "c" }; Assert.That( iarray, Has.Member(3) ); Assert.That( sarray, Has.Member("b") ); Assert.That( sarray, Contains.Item("c") ); Assert.That( sarray, Has.No.Member("x") );
Tests that two collections are equivalent - that they contain the same items, in any order.
CollectionEquivalentConstraint( IEnumerable other )
Is.EquivalentTo( IEnumerable other )
int[] iarray = new int[] { 1, 2, 3 }; string[] sarray = new string[] { "a", "b", "c" }; Assert.That( new string[] { "c", "a", "b" }, Is.EquivalentTo( sarray ) ); Assert.That( new int[] { 1, 2, 2 }, Is.Not.EquivalentTo( iarray ) );
Tests that one collection is a subset of another.
CollectionSubsetConstraint( ICollection )
Is.SubsetOf( IEnumerable )
int[] iarray = new int[] { 1, 2, 3 }; Assert.That( new int[] { 1, 3 }, Is.SubsetOf( iarray ) );
Tests that a collection is ordered.
CollectionOrderedConstraint()
Is.Ordered
...Descending ...By(string propertyName) ...Using(IComparer comparer) ...Using(IComparer<T> comparer) ...Using (Comparison<T> comparer)
int[] iarray = new int[] { 1, 2, 3 }; string[] sarray = new string[] { "c", "b", "a" }; string[] sarray2 = new string[] ( "a", "aa", "aaa" ); Assert.That( iarray, Is.Ordered ); Assert.That( sarray, Is.Ordered.Descending ); Assert.That( sarray2, Is.Ordered.By("Length");