Not Constraint
NotConstraint negates another constraint. It succeeds when the inner constraint fails, and fails when the inner
constraint succeeds.
Usage
Is.Not.<constraint>
Does.Not.<constraint>
Has.No.<constraint>
Examples
[Test]
public void NotConstraint_Examples()
{
// Basic negation
Assert.That(42, Is.Not.EqualTo(0));
Assert.That("Hello", Is.Not.Null);
Assert.That("Hello", Is.Not.Empty);
// String negation
Assert.That("Hello World", Does.Not.Contain("Goodbye"));
Assert.That("test.txt", Does.Not.EndWith(".pdf"));
// Collection negation
Assert.That(new[] { 1, 2, 3 }, Has.No.Member(0));
Assert.That(new[] { 1, 2, 3 }, Is.Not.Empty);
Assert.That(new[] { 1, 2, 3 }, Has.None.Negative);
// Type negation
Assert.That("Hello", Is.Not.InstanceOf<int>());
// Combined with And/Or
object obj = 42;
Assert.That(obj, Is.Not.Null.And.InstanceOf<int>());
Assert.That("test", Is.Not.Null.And.Not.Empty);
}
Notes
Is.Not,Does.Not, andHas.Noare all ways to negate constraints.Has.Nois specifically for collection constraints (equivalent toHas.None).- Negation applies to the immediately following constraint.
See Also
- And Constraint - Combine constraints
- Or Constraint - Alternative constraints