logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class AsyncAssert

    Truly awaitable exception assertions for async code under test.

    Inheritance
    object
    AsyncAssert
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: Serenity.Tests.Shared
    Assembly: Tests.dll
    Syntax
    public static class AsyncAssert
    Remarks

    NUnit's Assert.ThrowsAsync/Assert.CatchAsync/Assert.DoesNotThrowAsync block the calling thread while waiting for the delegate's task (sync-over-async). Under Unity's single-threaded synchronization context that blocks the main thread — so any tested delegate that actually YIELDS (a Task.Delay, a timeout, real asynchronous I/O) deadlocks the editor: the continuation needs the main thread the assert is blocking. These helpers await the delegate instead and never block; call them with await from a test declared public async Task.

    Methods

    CatchAsync<TException>(Func<Task>, string)

    Asserts that awaiting action throws TException or a derived type (matching NUnit's Assert.CatchAsync semantics).

    Declaration
    public static Task<TException> CatchAsync<TException>(Func<Task> action, string message = null) where TException : Exception
    Parameters
    Type Name Description
    Func<Task> action

    The asynchronous code expected to throw.

    string message

    Optional context appended to the failure message.

    Returns
    Type Description
    Task<TException>

    The caught exception, for further asserts.

    Type Parameters
    Name Description
    TException

    DoesNotThrowAsync(Func<Task>, string)

    Asserts that awaiting action completes without throwing.

    Declaration
    public static Task DoesNotThrowAsync(Func<Task> action, string message = null)
    Parameters
    Type Name Description
    Func<Task> action

    The asynchronous code expected NOT to throw.

    string message

    Optional context appended to the failure message.

    Returns
    Type Description
    Task

    ThrowsAsync<TException>(Func<Task>, string)

    Asserts that awaiting action throws EXACTLY TException (derived types fail, matching NUnit's Assert.ThrowsAsync semantics).

    Declaration
    public static Task<TException> ThrowsAsync<TException>(Func<Task> action, string message = null) where TException : Exception
    Parameters
    Type Name Description
    Func<Task> action

    The asynchronous code expected to throw.

    string message

    Optional context appended to the failure message.

    Returns
    Type Description
    Task<TException>

    The caught exception, for further asserts.

    Type Parameters
    Name Description
    TException
    In this article
    © 2026 Serenity. All Rights Reserved