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 の拡張フック」を参照してください。

適用対象