Assert.That Property

Definition

Gets the singleton instance of the Assert functionality.

public static Microsoft.VisualStudio.TestTools.UnitTesting.Assert That { get; }
static member That : Microsoft.VisualStudio.TestTools.UnitTesting.Assert
Public Shared ReadOnly Property That As Assert

Property Value

Examples

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

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public static class CustomAssertExtensions
{
    public static void IsPrime(this Assert assert, int value)
    {
        if (value < 2 || Enumerable.Range(2, (int)Math.Sqrt(value) - 1).Any(i => value % i == 0))
        {
            throw new AssertFailedException($"Assert.That.IsPrime failed. Value <{value}> is not a prime number.");
        }
    }
}

[TestClass]
public class CalculatorTests
{
    [TestMethod]
    public void NextPrime_ReturnsPrime()
    {
        int result = new Calculator().NextPrime(10);
        Assert.That.IsPrime(result);
    }
}

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 IsOfType<T>(this Assert assert, object obj) and the call-site would be Assert.That.IsOfType<Dog>(animal);. For more information, see Create custom assertions with Assert.That.

Applies to