Table of Contents

Class SseHasEventAssertion

Namespace
SseAssertions.TUnit
Assembly
SseAssertions.TUnit.dll

Fluent TUnit assertion that walks a Server-Sent Events wire-format string and verifies the presence (or count) of frames matching a given event-type name. Constructed by the TUnit source generator from the HasSseEvent(string) extension on string; the narrower methods (WithData, WithDataParsedAs<T>, WithId, WithRetryMillis) constrain which frames count and the count terminators (AtLeast, AtMost, Exactly) close the assertion.

[AssertionExtension("HasSseEvent")]
public sealed class SseHasEventAssertion : Assertion<string>, IAssertion
Inheritance
Assertion<string>
SseHasEventAssertion
Implements
IAssertion
Inherited Members
Assertion<string>.AssertAsync()
Assertion<string>.GetAwaiter()
Assertion<string>.And
Assertion<string>.Or

Remarks

The chain runs against the receiver string parsed via Parse(string) inside CheckAsync(EvaluationMetadata<string>); the matching itself is delegated to the shared SseAssertions.TUnit.SseEventMatcher so the Stream and HttpResponseMessage chains produce identically shaped diagnostics.

Constructors

SseHasEventAssertion(AssertionContext<string>, string)

Initialises the assertion. Called by the TUnit source generator.

public SseHasEventAssertion(AssertionContext<string> context, string eventName)

Parameters

context AssertionContext<string>

The assertion context supplied by TUnit.

eventName string

The SSE event-type name to look for. Matched case-sensitively against EventName.

Exceptions

ArgumentNullException

eventName is null.

Methods

AtLeast(int)

Expects at least count matching frames.

public SseHasEventAssertion AtLeast(int count)

Parameters

count int

The minimum match count. Must be non-negative.

Returns

SseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

AtMost(int)

Expects at most count matching frames.

public SseHasEventAssertion AtMost(int count)

Parameters

count int

The maximum match count. Must be non-negative.

Returns

SseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

CheckAsync(EvaluationMetadata<string>)

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<string> metadata)

Parameters

metadata EvaluationMetadata<string>

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

Returns

Task<AssertionResult>

The result of the assertion check

Exactly(int)

Expects exactly count matching frames.

public SseHasEventAssertion Exactly(int count)

Parameters

count int

The exact match count. Must be non-negative.

Returns

SseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

GetExpectation()

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

protected override string GetExpectation()

Returns

string

WithData(Func<string, bool>)

Narrows the assertion to frames whose Data satisfies the supplied predicate.

public SseHasEventAssertion WithData(Func<string, bool> predicate)

Parameters

predicate Func<string, bool>

The data predicate. Called with each candidate frame's Data value; frames for which the predicate returns true are counted.

Returns

SseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

predicate is null.

WithDataParsedAs<T>(Func<string, T>, Func<T, bool>)

Narrows the assertion to frames whose Data deserializes via parse into a T that satisfies predicate.

public SseHasEventAssertion WithDataParsedAs<T>(Func<string, T> parse, Func<T, bool> predicate)

Parameters

parse Func<string, T>

The deserializer applied to each candidate frame's Data. Supply a reflection-free parser (for example a source-generated JsonSerializer.Deserialize with a JsonTypeInfo<T>) so the assertion stays AOT-compatible. If it throws, the assertion fails naming the deserializer exception and the offending data.

predicate Func<T, bool>

The predicate applied to the parsed value; frames for which it returns true are counted.

Returns

SseHasEventAssertion

This assertion for chaining.

Type Parameters

T

The type the frame data is parsed into.

Exceptions

ArgumentNullException

parse or predicate is null.

WithId(string)

Narrows the assertion to frames carrying the given id: directive.

public SseHasEventAssertion WithId(string id)

Parameters

id string

The expected Id. Matched case-sensitively; a frame without an id: directive never matches.

Returns

SseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

id is null.

WithRetryMillis(Func<int?, bool>)

Narrows the assertion to frames whose RetryMillis satisfies the supplied predicate.

public SseHasEventAssertion WithRetryMillis(Func<int?, bool> predicate)

Parameters

predicate Func<int?, bool>

The retry-value predicate. Called with each candidate frame's RetryMillis (null for frames without a retry: directive); frames for which it returns true are counted.

Returns

SseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

predicate is null.