Search⌘ K
AI Features

Asserting Types

Explore how to assert object types and inheritance relationships in NUnit unit tests. Understand the difference between exact types and is-a relationships, and learn to use NUnit's TypeOf and InstanceOf assertions to effectively test polymorphism and class inheritance in your code.

Introduction

The essence of object-oriented code is summarized as code that has elements of inheritance, encapsulation, and polymorphism. Each of these concepts can be summarized as follows:

  • Inheritance is an abstraction for sharing similarities among classes.
  • Encapsulation is the grouping some related data items and the methods that operate on that data into a single unit.
  • Polymorphism allows access to objects of different types through the same interface.

In the context of inheritance, it is important to distinguish whether an instance is an exact type or whether an instance is an “is-a” type. This distinction is important since it sometimes informs the way tests are written.

Quick recap on inheritance

When speaking about ...