Skip to content

Make HubConnection more testable #34843

@BrennanConroy

Description

@BrennanConroy

This came up in the Hub Proxy source generator work, in order to easily test the source generator, we need to be able to mock the HubConnection.

The proposal is to add virtual to some of the public methods, highlighted below are the ones required for the source generator testing, but we might want to extend it to more of the public methods as well for general testability.

public class HubConnection : IAsyncDisposable
{
    Task StartAsync(CancellationToken cancellationToken = default);
    Task StopAsync(CancellationToken cancellationToken = default);
    ValueTask DisposeAsync();
+   virtual IDisposable On(string methodName, Type[] parameterTypes, Func<object?[], object, Task> handler, object state);
    void Remove(string methodName);
+   virtual Task<ChannelReader<object?>> StreamAsChannelCoreAsync(string methodName, Type returnType, object?[] args, CancellationToken cancellationToken = default);
+   virtual Task<object?> InvokeCoreAsync(string methodName, Type returnType, object?[] args, CancellationToken cancellationToken = default);
+   virtual Task SendCoreAsync(string methodName, object?[] args, CancellationToken cancellationToken = default);
+   virtual IAsyncEnumerable<TResult> StreamAsyncCore<TResult>(string methodName, object?[] args, CancellationToken cancellationToken = default);
}

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-signalrIncludes: SignalR clients and servers

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions