...
/Assertions and Test Cases for Enumerables and Collections
Assertions and Test Cases for Enumerables and Collections
Learn when and how to assert lists and objects contained in lists.
We'll cover the following...
Introduction
Grouping objects into collections and performing actions on this collection are one of the most fundamental programming tasks. Given its importance, it’s helpful to recall the ICollection class and some of its capabilities.
The ICollection class is a base interface for all other classes in the System.Collections namespace. The generic equivalent is the System.Collections.Generic.ICollection<T> interface. The ICollection interface itself extends the IEnumerable interface. This basic schematic is shown in the figure below:
The content covered below will cover a wide variety of assertions and demonstrate the flexibility of enumerable and collection-based assertions.
Catalog of collection assertions
NUnit provides an expressive vocabulary to assert collections. This expressiveness is derived from NUnit’s extensive collection of constraint assertions outlined below.
The AllItems constraint
These assertions, in turn, assert all items within an IEnumerable according to another supplied constraint. Its syntax is as follows:
Is.All...
Has.All...
There is no difference between Is.All and Has.All. The following example demonstrates the constraint:
using System;
namespace Project
{
class Program
{
static void Main(string[] args)
{
;
}
}
}
The AnyOf constraint
These assertions are used to determine whether a value is equal to any of the expected values.
Is.AnyOf(object[] expected)
These assertions come with certain modifiers, which are included with the constraint to enhance their expressiveness.
The following example demonstrates the constraint:
The CollectionContains constraint
These assertions test that an ...