Table of Contents

Class VhTestUtil

Namespace
VpnHood.Net.Toolkit.Utils
Assembly
VpnHood.Net.Toolkit.dll

Provides test assertions that wait for a value, or check the exception an API call throws.

public static class VhTestUtil
Inheritance
VhTestUtil
Inherited Members

Remarks

A wait reads the value every 100 milliseconds until it matches or the timeout passes; a read under way is not cut by the timeout, as it may still bring the value. The caller's token ends the wait for an asynchronous read, not a synchronous factory, nor the task itself.

Methods

AssertAlreadyExistsException(Task, string?, string?)

Awaits a task that must throw what tells an object exists already (Is(Exception)), or an ApiException that names AlreadyExistsException.

public static Task<Exception> AssertAlreadyExistsException(Task task, string? message = null, string? contains = null)

Parameters

task Task

The task.

message string

Added to the failure's message, or null.

contains string

Text the exception's message must hold, ignoring case, or null.

Returns

Task<Exception>

A task whose result is the exception.

Exceptions

VhTestUtil.AssertException

The task succeeded, or threw another kind.

Exception

The exception's message does not hold the text.

AssertApiException(int, Task, string?, string?)

Awaits a task that must throw an ApiException with a status code.

public static Task<Exception> AssertApiException(int expectedStatusCode, Task task, string? message = null, string? contains = null)

Parameters

expectedStatusCode int

The status code.

task Task

The task.

message string

Added to the failure's message, or null.

contains string

Text the exception's message must hold, ignoring case, or null.

Returns

Task<Exception>

A task whose result is the exception.

Exceptions

VhTestUtil.AssertException

The task succeeded.

Exception

The task threw with another status code or message, or threw another exception, which passes through.

AssertApiException(HttpStatusCode, Task, string?)

Awaits a task that must throw an ApiException with a status code.

public static Task<Exception> AssertApiException(HttpStatusCode expectedStatusCode, Task task, string? message = null)

Parameters

expectedStatusCode HttpStatusCode

The status code.

task Task

The task.

message string

Added to the failure's message, or null.

Returns

Task<Exception>

A task whose result is the exception.

Exceptions

VhTestUtil.AssertException

The task succeeded.

Exception

The task threw with another status code.

AssertApiException(string, Task, string?, string?)

Awaits a task that must throw an exception of a type's name, or an ApiException that names it.

public static Task<Exception> AssertApiException(string expectedExceptionType, Task task, string? message = null, string? contains = null)

Parameters

expectedExceptionType string

The exception type's name.

task Task

The task.

message string

Added to the failure's message, or null.

contains string

Text the exception's message must hold, ignoring case, or null.

Returns

Task<Exception>

A task whose result is the exception.

Exceptions

VhTestUtil.AssertException

The task succeeded, or threw another type.

Exception

The exception's message does not hold the text.

AssertApiException<T>(Task, string?, string?)

Awaits a task that must throw an exception of a type, or an ApiException that names it; the type's simple name is compared, not its identity.

public static Task<Exception> AssertApiException<T>(Task task, string? message = null, string? contains = null)

Parameters

task Task

The task.

message string

Added to the failure's message, or null.

contains string

Text the exception's message must hold, ignoring case, or null.

Returns

Task<Exception>

A task whose result is the exception.

Type Parameters

T

The exception's type.

Exceptions

VhTestUtil.AssertException

The task succeeded, or threw another type.

Exception

The exception's message does not hold the text.

AssertEqualsWait<TValue>(TValue, Func<Task<TValue>>, string?, int, bool, CancellationToken)

Waits until a value read asynchronously matches, and fails when it does not by the timeout.

public static Task AssertEqualsWait<TValue>(TValue expectedValue, Func<Task<TValue>> valueFactory, string? message = null, int timeout = 5000, bool noTimeoutOnDebugger = true, CancellationToken cancellationToken = default)

Parameters

expectedValue TValue

The expected value.

valueFactory Func<Task<TValue>>

Reads the value.

message string

The failure's message, or null for Unexpected Value.

timeout int

How long to wait, in milliseconds. The default is 5000.

noTimeoutOnDebugger bool

true to wait a day while a debugger is attached.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that completes when the value matches.

Type Parameters

TValue

The type of the value.

Exceptions

VhTestUtil.AssertException

The value still differs at the timeout.

ArgumentOutOfRangeException

timeout is negative, other than -1.

OperationCanceledException

cancellationToken was canceled.

AssertEqualsWait<TValue>(TValue, Func<TValue>, string?, int, bool, CancellationToken)

Waits until a value matches, and fails when it does not by the timeout.

public static Task AssertEqualsWait<TValue>(TValue expectedValue, Func<TValue> valueFactory, string? message = null, int timeout = 5000, bool noTimeoutOnDebugger = true, CancellationToken cancellationToken = default)

Parameters

expectedValue TValue

The expected value.

valueFactory Func<TValue>

Reads the value.

message string

The failure's message, or null for Unexpected Value.

timeout int

How long to wait, in milliseconds. The default is 5000.

noTimeoutOnDebugger bool

true to wait a day while a debugger is attached.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that completes when the value matches.

Type Parameters

TValue

The type of the value.

Exceptions

VhTestUtil.AssertException

The value still differs at the timeout.

ArgumentOutOfRangeException

timeout is negative, other than -1.

OperationCanceledException

cancellationToken was canceled.

AssertEqualsWait<TValue>(TValue, Task<TValue>, string?, int, bool, CancellationToken)

Waits for a task, and fails when its result does not match.

public static Task AssertEqualsWait<TValue>(TValue expectedValue, Task<TValue> task, string? message = null, int timeout = 5000, bool noTimeoutOnDebugger = true, CancellationToken cancellationToken = default)

Parameters

expectedValue TValue

The expected value.

task Task<TValue>

The task.

message string

The failure's message, or null for Unexpected Value.

timeout int

How long to wait, in milliseconds. The default is 5000.

noTimeoutOnDebugger bool

true to wait a day while a debugger is attached.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

A task that completes when the value matches.

Type Parameters

TValue

The type of the value.

Exceptions

VhTestUtil.AssertException

The value still differs at the timeout.

ArgumentOutOfRangeException

timeout is negative, other than -1.

OperationCanceledException

cancellationToken was canceled.

AssertNotExistsException(Task, string?, string?)

Awaits a task that must throw what tells an object was not found (Is(Exception)), or an ApiException that names NotExistsException.

public static Task<Exception> AssertNotExistsException(Task task, string? message = null, string? contains = null)

Parameters

task Task

The task.

message string

Added to the failure's message, or null.

contains string

Text the exception's message must hold, ignoring case, or null.

Returns

Task<Exception>

A task whose result is the exception.

Exceptions

VhTestUtil.AssertException

The task succeeded, or threw another kind.

Exception

The exception's message does not hold the text.