BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Testing
.NET 1.1+

NUnit Collection Assertions

The basic assertions provided by the NUnit framework can be used for all unit tests. To make unit tests more readable, there are other assertion types that more clearly express the intent of the tests. This article describes the collection assertions.

AllItemsAreNotNull

When a collection must not include any nulls, you can test it with the AllItemsAreNotNull assertion. This fails if one or more null references exist in the provided collection.

The sample test below checks that, when a missing contact is retrieved, a null Person is not added to the cache. This assumes that the RetrieveContact method returns null if an unknown contact is requested from the database.

[Test]
public void WhenRetrievingAMissingContactTheNullIsNotCached()
{
    IList<Person> cache = new List<Person>();

    Person active = RetrieveContact("Missing", cache);

    CollectionAssert.AllItemsAreNotNull(cache);
}

AllItemsAreInstancesOfType

For collections that can contain multiple types, you might need to check that all of the items in the sequence are of the same, specified type. You can do this with AllItemsAreInstancesOfType. The first argument is the collection to evaluate. The second is the type desired. If any object in the array is null or of a type that is neither the specified type nor a subclass thereof, the assertion fails.

The test below uses the assertion to create an alternative test to that of the previous example. By definition, all of the items in the collection must be of the Person type or one of its subclasses. However, if an item in the cache is null, the test will fail.

[Test]
public void WhenRetrievingAMissingContactTheNullIsNotCached()
{
    IList<Person> cache = new List<Person>();
 
    Person active = RetrieveContact("Missing", cache);

    CollectionAssert.AllItemsAreInstancesOfType(cache, typeof(Person));
}

AllItemsAreUnique

You might guess that AllItemsAreUnique passes if every value or reference in a collection is unique, and fails where there are duplicates. The test below shows this. Here we are checking that contacts retrieved from the database twice only appear once in the cache.

[Test]
public void RepeatedlyRetrievedContactsAreOnlyCachedOnce()
{
    IList<Person> cache = new List<Person>();

    Person active = RetrieveContact("Bob", cache);
    Person active2 = RetrieveContact("Bob", cache);

    CollectionAssert.AllItemsAreInstancesOfType(cache, typeof(Person));
}

Other Collection Assertions

There are two other useful assertions provided by CollectionAssert. IsEmpty passes if the provided sequence contains no items. IsNotEmpty passes if the collection contains at least one element.

The final example checks that a newly instantiated Person object has no related contacts.

[Test]
public void ANewPersonHasNoRelatedContacts()
{
    Person person = new Person();

    CollectionAssert.IsEmpty(person.RelatedContacts);
}
12 December 2013