Assert Value Categories
Learn and practice how to perform assertions on 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 ofnull
. 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:
To calculate the roots of the polynomial, we use the two equations:
It is possible that the value of = in which case there is one root and not two.
The component within the two equations is called the discriminant.
The value of the discriminant determines the nature of the roots as follows:
: There are no real roots
: There is one real root
: 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 1400+ tech skills courses.