Assert Value Categories

Introduction

C# types store values which are categorized into the following groups:

  • Null
  • NotNull
  • Zero
  • NotZero
  • IsNaN
  • IsEmpty
  • IsNotEmpty

NUnit provides facilities to assert against these value categories.

Conceptually understanding each value category

The following are interpretations of each value category:

  • Null: A nullable value type or a reference type can assume the value of null. This means that the variable does not point to any instance in memory.

  • NotNull: A nullable value type/reference type can assume the value of not null. This means that the variable points to an instance in memory.

  • Zero: A variable that assumes the value of zero.

  • NotZero: A variable that does not assume the value of zero.

  • IsNaN: A numeric variable assumes a value that is not expressible as a valid number. This scenario results from certain mathematical operations.

  • IsEmpty: The variable points to an instance that has zero length.

  • IsNotEmpty: The variable points to an instance whose length is not zero.

Value category assertions

Classic assertions for the various value categories are listed below. These methods are overloaded to take various other parameters. For a full list of overloaded methods, consult the official NUnit documentation, provided in the “Unit Testing Resources” lesson in the “Conclusion” section.

Null

The following syntax asserts whether the passed reference variable points to null:

Assert.Null(object anObject);

NotNull

The following syntax asserts whether the passed reference variable does not point to null:

Assert.NotNull(object anObject);

Zero

The following syntax asserts whether the passed value variable is equal to zero. Note that this method is overloaded to take other value types, not just integers.

Assert.Zero(int actual);

NotZero

The following syntax asserts whether the passed value variable is not equal to zero. Note that this method is overloaded to take other value types, not just integers.

Assert.NotZero(int actual);

IsNaN

The following syntax asserts whether the passed value variable is not a number:

Assert.IsNaN(double aDouble);

IsEmpty

The following syntax asserts whether the passed string or collection is empty:

Assert.IsEmpty(string aString);
Assert.IsEmpty(IEnumerable collection);

IsNotEmpty

The following syntax asserts whether the passed string or collection is not empty

Assert.IsNotEmpty(string aString);
Assert.IsNotEmpty(IEnumerable collection);

Exercise

In the next piece of code, we present a quadratic class. This class calculates the roots of a quadratic equation. A quadratic equation takes the following form:

f(x)=ax2+bx+cf(x) = ax^2 + bx + c

To calculate the roots of the polynomial, we use the two equations:

r1=b+b24ac2ar_1 = \frac{-b + \sqrt{b^2 - 4ac}}{2a}

r2=bb24ac2ar_2 = \frac{-b - \sqrt{b^2 - 4ac}}{2a}

It is possible that the value of r1r_1 = r2r_2 in which case there is one root and not two.

The component b24acb^2 - 4ac within the two equations is called the discriminant.

The value of the discriminant determines the nature of the roots as follows:

b24ac<0b^2 - 4ac < 0: There are no real roots

b24ac=0b^2 - 4ac = 0: There is one real root

b24ac>0b^2 - 4ac > 0: There are two real roots

The number of roots is visualized by the number of times the graph intersects the x-axis (horizontal axis).

These three cases are shown below:

Get hands-on with 1200+ tech skills courses.