StringAssert.That 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 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 上的擴充鉤子。