Substring Constraint
SubstringConstraint tests that a string contains the expected substring.
Usage
Does.Contain(string expected)
Does.Not.Contain(string expected)
Modifiers
.IgnoreCase
.Using(StringComparison comparisonType)
.Using(CultureInfo culture)
Examples
[Test]
public void SubstringConstraint_Examples()
{
string phrase = "Make your tests fail before passing!";
Assert.That(phrase, Does.Contain("tests"));
Assert.That(phrase, Does.Contain("TESTS").IgnoreCase);
Assert.That(phrase, Does.Not.Contain("missing"));
}
Specifying a StringComparison
[Test]
public void SubstringConstraint_StringComparison_Examples()
{
// IgnoreCase modifier
Assert.That("Hello World!", Does.Contain("WORLD").IgnoreCase);
Assert.That("Hello World!", Does.Contain("World"));
}
Specifying a CultureInfo
The Using(CultureInfo) modifier allows for culture-specific string comparisons.
It can be combined with .IgnoreCase for case-insensitive culture-aware comparisons:
// Using Turkish culture where 'i' and 'I' have special casing rules
Assert.That("Hello TITLE World", Does.Contain("title").IgnoreCase.Using(new CultureInfo("tr-TR")));
// Culture-specific comparison without case-insensitivity
Assert.That("Straße Street", Does.Contain("Straße").Using(new CultureInfo("de-DE")));
Notes
- Only one
Usingmodifier may be specified. Attempting to use multipleUsingmodifiers will throw anInvalidOperationException. - When using
Does.Contain()with a non-string actual value, it tests for collection membership instead. See ContainsConstraint for details.