Class ServerStreamAssertion<TResponse>
- Namespace
- GrpcAssertions.TUnit
- Assembly
- GrpcAssertions.TUnit.dll
Fluent TUnit assertion over the responses of an AsyncServerStreamingCall<TResponse>.
Constructed by the Streams() entry point on a value assertion source; the chain methods
(StreamsAtLeast, StreamsExactly, StreamContains) accumulate expectations and
AndStreamItems runs follow-on assertions against the materialized responses.
public sealed class ServerStreamAssertion<TResponse> : Assertion<AsyncServerStreamingCall<TResponse>>, IAssertion
Type Parameters
TResponseThe streamed response message type.
- Inheritance
-
Assertion<AsyncServerStreamingCall<TResponse>>ServerStreamAssertion<TResponse>
- Implements
-
IAssertion
- Inherited Members
-
Assertion<AsyncServerStreamingCall<TResponse>>.AssertAsync()Assertion<AsyncServerStreamingCall<TResponse>>.GetAwaiter()Assertion<AsyncServerStreamingCall<TResponse>>.AndAssertion<AsyncServerStreamingCall<TResponse>>.Or
Remarks
The response stream is read exactly once, when the assertion is awaited: every accumulated
expectation, and the optional AndStreamItems callback, runs against the single materialized
list. A stream that faults mid-read (an RpcException from the reader) fails the
assertion with the partial count and the rendered gRPC outcome.
Constructors
ServerStreamAssertion(AssertionContext<AsyncServerStreamingCall<TResponse>>, CancellationToken)
Initializes the assertion. Called by the Streams() entry point.
public ServerStreamAssertion(AssertionContext<AsyncServerStreamingCall<TResponse>> context, CancellationToken cancellationToken)
Parameters
contextAssertionContext<AsyncServerStreamingCall<TResponse>>The assertion context supplied by TUnit (the streaming call value).
cancellationTokenCancellationTokenThe token threaded into the response-stream read.
Methods
AndStreamItems(Func<IReadOnlyList<TResponse>, Task>)
Runs assertions against the materialized responses after the
accumulated count and content expectations pass. The responses are already read, so the
callback can assert on individual items without re-reading the stream.
public ServerStreamAssertion<TResponse> AndStreamItems(Func<IReadOnlyList<TResponse>, Task> assertions)
Parameters
assertionsFunc<IReadOnlyList<TResponse>, Task>The follow-on assertions, applied to the response list. Must not be null.
Returns
- ServerStreamAssertion<TResponse>
This assertion for chaining.
Exceptions
- ArgumentNullException
assertionsis null.
CheckAsync(EvaluationMetadata<AsyncServerStreamingCall<TResponse>>)
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<AsyncServerStreamingCall<TResponse>> metadata)
Parameters
metadataEvaluationMetadata<AsyncServerStreamingCall<TResponse>>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
StreamContains(Func<TResponse, bool>, string?)
Asserts at least one streamed response satisfies predicate.
public ServerStreamAssertion<TResponse> StreamContains(Func<TResponse, bool> predicate, string? predicateExpression = null)
Parameters
predicateFunc<TResponse, bool>The response predicate. Must not be null.
predicateExpressionstringThe source text of
predicate, captured by the compiler for the failure message.
Returns
- ServerStreamAssertion<TResponse>
This assertion for chaining.
Exceptions
- ArgumentNullException
predicateis null.
StreamsAtLeast(int)
Asserts the stream produces at least count responses.
public ServerStreamAssertion<TResponse> StreamsAtLeast(int count)
Parameters
countintThe minimum response count. Must be non-negative.
Returns
- ServerStreamAssertion<TResponse>
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis negative.
StreamsExactly(int)
Asserts the stream produces exactly count responses.
public ServerStreamAssertion<TResponse> StreamsExactly(int count)
Parameters
countintThe exact response count. Must be non-negative.
Returns
- ServerStreamAssertion<TResponse>
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis negative.