StringAssert.That 属性

定义

获取 StringAssert 功能的单一实例。

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

属性值

属性

示例

以下示例将自定义 ContainsWords 断言定义为扩展方法, StringAssert 并通过以下方法调用它 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" });
    }
}

注解

用户可以通过 C# 扩展方法插入自定义断言。 例如,自定义断言提供程序的签名可以是 public static void ContainsWords(this StringAssert customAssert, string value, ICollection substrings) 调用站点 StringAssert.That.ContainsWords(value, substrings);

对于新的自定义断言,首选扩展 That ,因为在 StringAssert 将来的版本中可能会弃用。 有关详细信息,请参阅 StringAssert 和 CollectionAssert 上的扩展挂钩

适用于