Contains Constraint
ContainsConstraint is a smart constraint that adapts its behavior based on the type being tested. When the actual
value is a string, it tests for a substring. When the actual value is a collection, it tests for item membership.
Usage
Does.Contain(object expected)
Does.Not.Contain(object expected)
Modifiers
.IgnoreCase // Case-insensitive matching (strings and collections)
.IgnoreWhiteSpace // Ignore whitespace in item equality (collections only)
.IgnoreLineEndingFormat // Ignore line endings in item equality (collections only)
Note
.IgnoreWhiteSpace and .IgnoreLineEndingFormat only work when testing collection membership, not substring
containment. For strings, these modifiers throw an InvalidOperationException.
Examples
String Containment
When the actual value is a string, Does.Contain tests for a substring:
[Test]
public void ContainsConstraint_String_Examples()
{
Assert.That("Hello World", Does.Contain("World"));
Assert.That("Hello World", Does.Contain("world").IgnoreCase);
Assert.That("Hello World", Does.Not.Contain("Goodbye"));
}
Collection Membership
When the actual value is a collection, Does.Contain tests for item membership:
[Test]
public void ContainsConstraint_Collection_Examples()
{
int[] numbers = { 1, 2, 3, 4, 5 };
Assert.That(numbers, Does.Contain(3));
Assert.That(numbers, Does.Not.Contain(99));
string[] names = { "Alice", "Bob", "Charlie" };
Assert.That(names, Does.Contain("Bob"));
Assert.That(names, Does.Contain("bob").IgnoreCase);
}
How It Works
ContainsConstraint defers its behavior until the actual value's type is known:
| Actual Type | Behavior |
|---|---|
string |
Uses SubstringConstraint to test for substring |
| Collection/IEnumerable | Uses SomeItemsConstraint to test for item presence |
Notes
When testing strings, the expected value must also be a string. For type safety, consider using
Does.Contain(string)explicitly.Important:
.IgnoreWhiteSpaceand.IgnoreLineEndingFormatonly work with collections, not strings. When used with string containment, they throwInvalidOperationException. This is becauseSubstringConstraintdoesn't support these modifiers, whileEqualConstraint(used for collection item comparison) does.For collections of strings,
.IgnoreWhiteSpaceaffects item equality comparison:// Works: checks if any item equals "hello world" ignoring whitespace Assert.That(new[] { "hello world" }, Does.Contain("hello world").IgnoreWhiteSpace);For more explicit control, use the specific constraints directly:
- SubstringConstraint for string containment
- SomeItemsConstraint for collection membership