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>.AndAssertion<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
contextAssertionContext<string>The assertion context supplied by TUnit.
eventNamestringThe SSE event-type name to look for. Matched case-sensitively against EventName.
Exceptions
- ArgumentNullException
eventNameis null.
Methods
AtLeast(int)
Expects at least count matching frames.
public SseHasEventAssertion AtLeast(int count)
Parameters
countintThe minimum match count. Must be non-negative.
Returns
- SseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis negative.
AtMost(int)
Expects at most count matching frames.
public SseHasEventAssertion AtMost(int count)
Parameters
countintThe maximum match count. Must be non-negative.
Returns
- SseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis 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
metadataEvaluationMetadata<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
countintThe exact match count. Must be non-negative.
Returns
- SseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis negative.
GetExpectation()
Gets a human-readable description of what this assertion expects. Used in error messages.
protected override string GetExpectation()
Returns
WithData(Func<string, bool>)
Narrows the assertion to frames whose Data satisfies the supplied predicate.
public SseHasEventAssertion WithData(Func<string, bool> predicate)
Parameters
predicateFunc<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
predicateis 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
parseFunc<string, T>The deserializer applied to each candidate frame's Data. Supply a reflection-free parser (for example a source-generated
JsonSerializer.Deserializewith aJsonTypeInfo<T>) so the assertion stays AOT-compatible. If it throws, the assertion fails naming the deserializer exception and the offending data.predicateFunc<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
TThe type the frame data is parsed into.
Exceptions
- ArgumentNullException
parseorpredicateis null.
WithId(string)
Narrows the assertion to frames carrying the given id: directive.
public SseHasEventAssertion WithId(string id)
Parameters
idstringThe expected Id. Matched case-sensitively; a frame without an
id:directive never matches.
Returns
- SseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentNullException
idis null.
WithRetryMillis(Func<int?, bool>)
Narrows the assertion to frames whose RetryMillis satisfies the supplied predicate.
public SseHasEventAssertion WithRetryMillis(Func<int?, bool> predicate)
Parameters
predicateFunc<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
predicateis null.