Table of Contents

Class GrpcExceptionAssertion

Namespace
GrpcAssertions.TUnit
Assembly
GrpcAssertions.TUnit.dll

Fluent TUnit assertion that a delegate threw a gRPC RpcException, optionally with an expected StatusCode and an expected Detail. Constructed by the ThrowsGrpcException() entry points on a delegate assertion source; the chain methods (WithDetail, WithDetailContaining, and the IsUnavailable() / IsNotFound() status shorthands) narrow the assertion and return the same instance.

public sealed class GrpcExceptionAssertion : Assertion<RpcException>, IAssertion
Inheritance
Assertion<RpcException>
GrpcExceptionAssertion
Implements
IAssertion
Inherited Members
Assertion<RpcException>.AssertAsync()
Assertion<RpcException>.GetAwaiter()
Assertion<RpcException>.And
Assertion<RpcException>.Or

Remarks

The delegate's caught exception is moved into the assertion value by TUnit's MapException<RpcException>(): when the delegate throws an RpcException it is the value, when it throws any other exception the value is null and the metadata exception is populated, and when it throws nothing both are null. A single CheckAsync(EvaluationMetadata<RpcException>) applies the accumulated status and detail expectations and renders the full gRPC outcome on failure.

Constructors

GrpcExceptionAssertion(AssertionContext<RpcException>, StatusCode?)

Initializes the assertion. Called by the ThrowsGrpcException() entry points.

public GrpcExceptionAssertion(AssertionContext<RpcException> context, StatusCode? expectedStatus)

Parameters

context AssertionContext<RpcException>

The assertion context supplied by TUnit (the mapped exception value).

expectedStatus StatusCode?

The expected status code, or null to accept any status.

Methods

CheckAsync(EvaluationMetadata<RpcException>)

Implements the specific check logic for this assertion. Called after the context has been evaluated. Override this method if your assertion uses the default AssertAsync() flow. If you override AssertAsync() with custom logic (like AndAssertion/OrAssertion), you don't need to implement this.

protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<RpcException> metadata)

Parameters

metadata EvaluationMetadata<RpcException>

Metadata about the evaluation including value, exception, and timing information

Returns

Task<AssertionResult>

The result of the assertion check

GetExpectation()

Gets a human-readable description of what this assertion expects. Used in error messages.

protected override string GetExpectation()

Returns

string

IsAborted()

Asserts the status is Aborted.

public GrpcExceptionAssertion IsAborted()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsAlreadyExists()

Asserts the status is AlreadyExists.

public GrpcExceptionAssertion IsAlreadyExists()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsCancelled()

Asserts the status is Cancelled.

public GrpcExceptionAssertion IsCancelled()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsDeadlineExceeded()

Asserts the status is DeadlineExceeded.

public GrpcExceptionAssertion IsDeadlineExceeded()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsFailedPrecondition()

Asserts the status is FailedPrecondition.

public GrpcExceptionAssertion IsFailedPrecondition()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsInternal()

Asserts the status is Grpc.Core.StatusCode.Internal.

public GrpcExceptionAssertion IsInternal()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsInvalidArgument()

Asserts the status is InvalidArgument.

public GrpcExceptionAssertion IsInvalidArgument()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsNotFound()

Asserts the status is NotFound.

public GrpcExceptionAssertion IsNotFound()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsOk()

Asserts the status is OK.

public GrpcExceptionAssertion IsOk()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsPermissionDenied()

Asserts the status is PermissionDenied.

public GrpcExceptionAssertion IsPermissionDenied()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsResourceExhausted()

Asserts the status is ResourceExhausted.

public GrpcExceptionAssertion IsResourceExhausted()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsUnauthenticated()

Asserts the status is Unauthenticated.

public GrpcExceptionAssertion IsUnauthenticated()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsUnavailable()

Asserts the status is Unavailable.

public GrpcExceptionAssertion IsUnavailable()

Returns

GrpcExceptionAssertion

This assertion for chaining.

IsUnimplemented()

Asserts the status is Unimplemented.

public GrpcExceptionAssertion IsUnimplemented()

Returns

GrpcExceptionAssertion

This assertion for chaining.

WithDetail(string)

Asserts that the exception's Detail exactly equals expected (ordinal comparison).

public GrpcExceptionAssertion WithDetail(string expected)

Parameters

expected string

The expected detail. Must not be null.

Returns

GrpcExceptionAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

expected is null.

InvalidOperationException

A detail expectation was already specified.

WithDetailContaining(string, StringComparison)

Asserts that the exception's Detail contains substring using comparison.

public GrpcExceptionAssertion WithDetailContaining(string substring, StringComparison comparison)

Parameters

substring string

The substring the detail must contain. Must not be null.

comparison StringComparison

The comparison used to locate the substring.

Returns

GrpcExceptionAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

substring is null.

InvalidOperationException

A detail expectation was already specified.

WithTrailer(string, ReadOnlySpan<byte>)

Asserts the exception's Trailers contain a binary entry at key (a -bin key) whose bytes equal value. Text entries are never matched by this overload; use WithTrailer(string, string) for those. A byte array converts to value implicitly.

public GrpcExceptionAssertion WithTrailer(string key, ReadOnlySpan<byte> value)

Parameters

key string

The binary trailer key (ends in -bin). Must not be null.

value ReadOnlySpan<byte>

The expected bytes.

Returns

GrpcExceptionAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

key is null.

WithTrailer(string, string)

Asserts the exception's Trailers contain a text entry at key whose value equals value (ordinal). Keys are matched case-insensitively (gRPC lowercases metadata keys). Binary (-bin) entries are never matched by this overload; use WithTrailer(string, ReadOnlySpan<byte>) for those.

public GrpcExceptionAssertion WithTrailer(string key, string value)

Parameters

key string

The trailer key. Must not be null.

value string

The expected text value. Must not be null.

Returns

GrpcExceptionAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

key or value is null.