Instead of using a different Assert method for each different type of assertion (e.g.:
Assert.AreEqual() for equality comparison, or
Assert.IsTrue() for boolean evaluation), the new Constraint-Based Assert Model introduced in NUnit 2.4 utilizes only one Assert method named
Assert.That().
This "one method" approach is possible due to the introduction of a set of what NUnit calls "syntax helpers" which are passed in as an argument to the method. You can also pass in "constraint objects", as they implement the IConstraint interface as well, however, this article will be focusing only on the "syntax helpers". Editoral Note: This new "one method" approach strikes me as an application of the GoF
Strategy Pattern.
The most common overloads to the Assert.That() method are as follows:
Assert.That( object actual, IConstraint constraint )
Assert.That( object actual, IConstraint constraint, string message )
Assert.That( object actual, IConstraint constraint, string message, object[] parms )
A "quick start" example could look like this (NOTE: for all these examples, be sure to include the reference
using NUnit.Framework.SyntaxHelpers;)":
Assert.That( myString, Is.EqualTo("Hello") );
As you could easily guess, this snippet of code asserts that the variable "myString" contains the string "Hello".
Constraint types
Equal ConstraintSameAs ConstraintCondition ConstraintsComparison ConstraintsType ConstraintsString ConstraintsCollection Constraints
Equal Constraint
To implement an equality assertion:
Assert.That(1 + 1, Is.EqualTo(2));
To implement an inequality assertion:
Assert.That(1 + 1, Is.Not.EqualTo(3));
To implement an equality assertion for floating point numbers and allow for a range of tolerance:
Assert.That(2.5000 + 2.5001, Is.EqualTo(5).Within(.0001));
To implement an equality assertion for strings in a case-insensitive manner:
Assert.That( "Hello", Is.EqualTo( "hello" ).IgnoreCase );
NOTE: In this section, I introduced the
Is.Not "syntax helper". It, in effect, returns the opposite of its partner
Is and it's usable in almost all the places that
Is ...well... is.
Also, some of you may have noticed that the "syntax helpers" in the last three examples (e.g.:
Is.EqualTo(5).Within(.0001) and
Is.EqualTo( "hello" ).IgnoreCase) displayed a syntax of "chained" methods/properties. This syntax is popularly known under the name of
Fluent Interfaces.
SameAs Constraint
To implement a "same referenced object" assertion:
Object o1 = new Object();
Object o2 = o1;
Assert.That(o1, Is.SameAs(o2));
Condition Constraints
To implement boolean assertions:
Assert.That(true, Is.True);
Assert.That(false, Is.False);
To implement "Not A Number" assertions:
Assert.That(Double.NaN, Is.NaN);
Assert.That(1, Is.Not.Nan);
To implement assertion for empty Strings or Collections:
Assert.That("", Is.Empty);
Assert.That(new ArrayList(), Is.Empty);
To implement assertion for Collections containing only unique items:
ArrayList al = new ArrayList();
al.Add(1);
al.Add(2);
Assert.That(al, Is.Unique);
Comparison Constraints
To implement comparison assertions:
Assert.That(7, Is.GreaterThan(3));
Assert.That(7, Is.GreaterThanOrEqualTo(3));
Assert.That(7, Is.AtLeast(3));
Assert.That(7, Is.GreaterThanOrEqualTo(7));
Assert.That(7, Is.AtLeast(7));
Assert.That(3, Is.LessThan(7));
Assert.That(3, Is.LessThanOrEqualTo(7));
Assert.That(3, Is.AtMost(7));
Assert.That(3, Is.LessThanOrEqualTo(3));
Assert.That(3, Is.AtMost(3));
Note:
Is.AtLeast() is a synonym for
Is.GreaterThanOrEqualTo() and
Is.AtMost() is a synonym for
Is.LessThanOrEqualTo().
Type Constraints
To implement an "exact type" assertion:
Hashtable ht = new Hashtable();
Assert.That(ht, Is.TypeOf(typeof(Hashtable)));
Assert.That(ht, Is.Not.TypeOf(typeof(IDictionary))); //not exact type
To implement an "instance of a type" assertion:
Hashtable ht = new Hashtable();
Assert.That(ht, Is.InstanceOfType(typeof(IDictionary)));
Assert.That(ht, Is.Not.InstanceOfType(typeof(String)));
To implement an "assignable from a type" assertion:
Hashtable ht = new Hashtable();
Assert.That(ht, Is.AssignableFrom(typeof(System.Configuration.SettingsContext))); //subclass
Assert.That(ht, Is.Not.AssignableFrom(typeof(IDictionary)));
String Constraints
To implement string assertions:
string phrase = "Make your tests fail before passing!";
Assert.That( phrase, Text.Contains( "tests fail" ) );
Assert.That( phrase, Text.Contains( "make" ).IgnoreCase );
Assert.That( phrase, Text.StartsWith( "Make" ) );
Assert.That( phrase, Text.DoesNotStartWith( "Break" ) );
Assert.That( phrase, Text.EndsWith( "!" ) );
Assert.That( phrase, Text.EndsWith( "PASSING!" ).IgnoreCase );
Assert.That( phrase, Text.Matches( "Make.*tests.*pass" ) );
Assert.That( phrase, Text.DoesNotMatch( "your.*passing.*tests" ) );
Collection Constraints
To implement a collection assertion where each item must pass the specified constraint:
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.InstanceOfType(typeof(string)));
Assert.That(iarray, Is.All.GreaterThan(0));
To implement a collection assertion where at least 1 item must pass the specified constraint:
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That(sarray, Has.Some.Not.Null);
Assert.That(sarray, Has.Some.InstanceOfType(typeof(string)));
Assert.That(iarray, Has.Some.GreaterThan(2));
To implement a collection assertion where each item must fail the specified constraint:
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That(sarray, Has.None.Null);
Assert.That(sarray, Has.None.InstanceOfType(typeof(double)));
Assert.That(iarray, Has.None.GreaterThan(5));
NOTE:
List.Contains() has been replaced by
Has.Member() in version 2.4.2.
To implement a "unique items only" collection assertion:
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That(sarray, Is.Unique);
To implement a "must contain this item" collection assertion:
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, Has.No.Member("x"));
NOTE:
Has.Member() uses object equality to find an object in a collection. To check for an object equal to an item the collection, use
Has.Some.EqualTo().
To implement an "equivalent" collection assertion:
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));
NOTE: 2 collections are equivalent if they contain the same items, in any order. To compare collections for
equality, use
Is.EqualTo()
To implement an "is a subset of" collection assertion:
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That(new int[] { 1, 3 }, Is.SubsetOf(iarray));
If you found this post helpful, please "Kick" it so others can find it too: