## Constraint-Based Assert Model (NUnit 2.4)

The constraint-based Assert model uses a single method of the Assert class for all assertions. The logic necessary to carry out each assertion is embedded in the constraint object passed as the second parameter to that method.

Here's a very simple assert using the constraint model:

          Assert.That( myString, Is.EqualTo("Hello") );

The second argument in this assertion uses one of NUnit's syntax helpers to create an EqualConstraint. The same assertion could also be made in this form:

          Assert.That( myString, new EqualConstraint("Hello") );

Using this model, all assertions are made using one of the forms of the Assert.That() method, which has a number of overloads...

    Assert.That( object actual, IResolveConstraint constraint )
    Assert.That( object actual, IResolveConstraint constraint,
                 string message )
    Assert.That( object actual, IResolveConstraint constraint,
                 string message, object[] parms )

    Assert.That( ActualValueDelegate del, IResolveConstraint constraint )
    Assert.That( ActualValueDelegate del, IResolveConstraint constraint,
                 string message )
    Assert.That( ActualValueDelegate del, IResolveConstraint constraint,
                 string message, object[] parms )

    Assert.That<T>( ref T actual, IResolveConstraint constraint )
    Assert.That<T>( ref T actual, IResolveConstraint constraint,
                 string message )
    Assert.That<T>( ref T actual, IResolveConstraint constraint,
                 string message, object[] parms )

    Assert.That( bool condition );
    Assert.That( bool condition, string message );
    Assert.That( bool condition, string message, object[] parms );

    Assert.That( TestDelegate del, IResolveConstraint constraint );

If you derive your test fixture class from AssertionHelper, the Expect() method may be used in place of Assert.That()...

    Expect( object actual, IResolveConstraint constraint )
    Expect( object actual, IResolveConstraint constraint,
                 string message )
    Expect( object actual, IResolveConstraint constraint,
                 string message, object[] parms )

    Expect( ActualValueDelegate del, IResolveConstraint constraint )
    Expect( ActualValueDelegate del, IResolveConstraint constraint,
                 string message )
    Expect( ActualValueDelegate del, IResolveConstraint constraint,
                 string message, object[] parms )

    Expect<T>( ref T actual, IResolveConstraint constraint )
    Expect<T>( ref T actual, IResolveConstraint constraint,
                 string message )
    Expect<T>( ref T actual, IResolveConstraint constraint,
                 string message, object[] parms )

    Expect( bool condition );
    Expect( bool condition, string message );
    Expect( bool condition, string message, object[] parms );

    Expect( TestDelegate del, IResolveConstraint constraint );

The overloads that take a bool work exactly like Assert.IsTrue.

For overloads taking a constraint, the argument must be a object implementing the IConstraint interface, which supports performing a test on an actual value and generating appropriate messages. This interface is described in more detail under Custom Constraints.

NUnit provides a number of constraint classes similar to the EqualConstraint used in the example above. Generally, these classes may be used directly or through a syntax helper. Test fixture classes inheriting from AssertionHelper are able to use shorter forms. The valid forms are described on the pages related to each constraint. Note that the menu items listed to the right generally reflect the names of the syntax helpers.

See also: the classic model of assertions.