StringAssert.That Property

Definition

Gets the singleton instance of the StringAssert functionality.

public static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert That { get; }
[System.Runtime.CompilerServices.Nullable(1)]
public static Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert That { get; }
static member That : Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert
[<System.Runtime.CompilerServices.Nullable(1)>]
static member That : Microsoft.VisualStudio.TestTools.UnitTesting.StringAssert
Public Shared ReadOnly Property That As StringAssert

Property Value

Attributes

Examples

The following example defines a custom ContainsWords assertion as an extension method on StringAssert and invokes it through StringAssert.That:

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public static class CustomStringAssertExtensions
{
    public static void ContainsWords(this StringAssert stringAssert, string value, IEnumerable<string> words)
    {
        foreach (string word in words)
        {
            if (value == null || !value.Contains(word))
            {
                throw new AssertFailedException($"StringAssert.That.ContainsWords failed. Word <{word}> not found in <{value}>.");
            }
        }
    }
}

[TestClass]
public class MessageTests
{
    [TestMethod]
    public void Greeting_ContainsExpectedWords()
    {
        StringAssert.That.ContainsWords("Hello, world!", new[] { "Hello", "world" });
    }
}

Remarks

Users can use this to plug-in custom assertions through C# extension methods. For instance, the signature of a custom assertion provider could be public static void ContainsWords(this StringAssert customAssert, string value, ICollection substrings) and the call-site would be StringAssert.That.ContainsWords(value, substrings);.

For new custom assertions, prefer extending That instead, because StringAssert is likely to be deprecated in a future release. For more information, see Extension hooks on StringAssert and CollectionAssert.

Applies to