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 Type Assertions

The ninth part of the Automated Unit Testing tutorial continues the examination of the assertion commands provided by the NUnit framework. This article describes the type assertions, which check the types of objects or structures.

Type Asserts

The NUnit type assertions can be used in your tests to check that the types of objects and structures are as expected. Two types of assert are available. Firstly, you can check that an object is of a specified type, or can be assigned to a variable of that type. Secondly, you can assert that an object could have been assigned from an object of a specified type.

Each of the type asserts that we will look at has a non-generic version that accepts the expected type as its first parameter and the object to test as the second argument. You can also use a generic method for each assertion, specifying the expected type as the type parameter. For the first example we will see both versions.

To begin, create the following two classes. We will execute tests against this simple inheritance hierarchy.

public class TeamMember
{
}

public class Programmer : TeamMember
{
}

Assert.IsInstanceOf

Assert.IsInstanceOf checks that an object is an instance of a specified type. The type can be the exact type of the object or one of its supertypes. You can think of the assertion as checking that the object can be assigned to the expected type. In the following example we create an instance of the Programmer class and assert that it is a Programmer using the non-generic method, which is compatible with the .NET framework version 1.1:

[Test]
public void NonGenericIsInstanceOf()
{
    TeamMember teamMember = new Programmer();
    Assert.IsInstanceOf(typeof(Programmer), teamMember);
}

The next example determines that the variable is an instance of TeamMember. This test passes ,even though the variable type and underlying object type are both "Programmer", because an object of the Programmer type can be assigned to a TeamMember variable.

[Test]
public void GenericIsInstanceOf()
{
    Programmer programmer = new Programmer();
    Assert.IsInstanceOf<TeamMember>(programmer);
}

Assert.IsNotInstanceOf

Assert.IsNotInstanceOf is the opposite of IsInstanceOf, in that it ensures that an object cannot be assigned to a variable of the expected type. You can see this in the following test, which passes, as a TeamMember object cannot be assigned to the Programmer type.

[Test]
public void GenericIsNotInstanceOf()
{
    TeamMember teamMember = new TeamMember();
    Assert.IsNotInstanceOf<Programmer>(teamMember);
}

Assert.IsAssignableFrom

Where IsInstanceOf can be used to assert that an object can be assigned to an expected type, the less commonly used IsAssignableFrom checks the reverse relationship. Tests containing such an assertion pass only when the object could be assigned a value that is of the expected type.

The test below checks that the variable, which is declared as a new TeamMember, could be assigned a value of the Programmer type. As Programmer is a subtype of TeamMember, this test passes.

[Test]
public void GenericIsAssignableFrom()
{
    TeamMember teamMember = new TeamMember();
    Assert.IsAssignableFrom<Programmer>(teamMember);
}

Assert.IsNotAssignableFrom

You can use the IsNotAssignableFrom assertion method to check that an object could not have been assigned from a given type. The example code shows a test that passes. Here we are asserting that a Programmer object could not be assigned a TeamMember value. Note that it is the actual type of the variable being tested (Programmer), not the declared type of the variable (TeamMember).

[Test]
public void GenericIsNotAssignableFrom()
{
    TeamMember teamMember = new Programmer();
    Assert.IsNotAssignableFrom<TeamMember>(teamMember);
}
17 April 2011