RpcCallSpy class

An IRpcInterceptor that records every RPC call for later inspection.

Attach to RpcTestApp or RpcApp to capture calls during tests, then assert on entries, callsTo, callCount, etc.

Retention. By default every call is kept forever, which is what a test wants and what a long-running server cannot afford: each RpcSpyEntry holds the call's RpcContext, so an unbounded spy on a live server pins one context graph per call for the life of the process. Pass maxEntries to keep only the most recent N (older entries are dropped as new ones arrive), or call reset periodically.

final spy = RpcCallSpy();
final app = await RpcTestApp.start(
  modules: [EchoModule()],
  interceptors: [spy],
);

final client = EchoCallerContract(app.caller);
await client.ping(PingRequest('hello'));
await client.ping(PingRequest('world'));

expect(spy.callCount, 2);
expect(spy.callsTo('EchoService', 'ping'), hasLength(2));
expect(spy.entries.first.success, isTrue);
expect(spy.wasCalled('EchoService', 'ping'), isTrue);

Constructors

RpcCallSpy({int? maxEntries})
Creates a spy.

Properties

callCount int
Total number of recorded calls.
no setter
entries List<RpcSpyEntry>
All recorded entries in call order.
no setter
errorCount int
Number of calls that threw an error.
no setter
hashCode int
The hash code for this object.
no setterinherited
maxEntries int?
Maximum entries retained, or null for unbounded.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
successCount int
Number of calls that completed successfully.
no setter

Methods

callsTo(String service, String method) List<RpcSpyEntry>
All entries for the given service and method.
interceptBidirectionalStream<TRequest, TResponse>(RpcMiddlewareContext call, Stream<TRequest> requests, RpcBidirectionalStreamNext<TRequest, TResponse> next) FutureOr<Stream<TResponse>>
Bidirectional-stream call interceptor.
interceptClientStream<TRequest, TResponse>(RpcMiddlewareContext call, Stream<TRequest> requests, RpcClientStreamNext<TRequest, TResponse> next) Future<TResponse>
Client-stream call interceptor.
interceptServerStream<TRequest, TResponse>(RpcMiddlewareContext call, TRequest request, RpcServerStreamNext<TRequest, TResponse> next) FutureOr<Stream<TResponse>>
Server-stream call interceptor.
interceptUnary<TRequest, TResponse>(RpcMiddlewareContext call, TRequest request, RpcUnaryNext<TRequest, TResponse> next) Future<TResponse>
Unary call interceptor.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reset() → void
Removes all recorded entries.
toString() String
A string representation of this object.
inherited
wasCalled(String service, String method) bool
Whether service.method was called at least once.

Operators

operator ==(Object other) bool
The equality operator.
inherited