Legacy Documentation. View NUnit 3 Documentation

Type Constraints (NUnit 2.4)

Type constraints perform tests that are specific to Types. The following type constraints are provided:

Syntax HelperConstructorOperation
Is.TypeOf( Type )ExactTypeConstraint( Type )tests that an object is an exact Type
Is.InstanceOfType( Type )InstanceOfTypeConstraint( Type )tests that an object is an instance of a Type
Is.AssignableFrom( Type )AssignableFromConstraint( Type )tests that one type is assignable from another

Examples of Use

Assert.That("Hello", Is.TypeOf(typeof(string)));
Assert.That("Hello", Is.Not.TypeOf(typeof(int)));

Assert.That("Hello", Is.InstanceOfType(typeof(string)));
Assert.That(5, Is.Not.InstanceOfType(typeof(string)));

Assert.That( "Hello", Is.AssignableFrom(typeof(string)));
Assert.That( 5, Is.Not.AssignableFrom(typeof(string)));

// Using inheritance
Expect( 5, Not.InstanceOfType(typeof(string)));
Expect( "Hello", AssignableFrom(typeOf(string)));