Null Constraint
NullConstraint tests that a value is null. This is one of the most commonly used constraints for verifying that
references have not been assigned or have been explicitly set to null.
Usage
Is.Null
Is.Not.Null
Examples
[Test]
public void NullConstraint_Examples()
{
object? obj = null;
Assert.That(obj, Is.Null);
string name = "Alice";
Assert.That(name, Is.Not.Null);
// Combining with other constraints
object? result = "value";
Assert.That(result, Is.Not.Null.And.Not.EqualTo(""));
}
Notes
- For value types, consider using
Is.Defaultinstead, which tests for the default value of the type. - When testing nullable value types (
int?,bool?, etc.),Is.Nullworks as expected.