Announcements icon indicating copy to clipboard operation
Announcements copied to clipboard

[Breaking change]: Unit testing SignalR Hubs may need updating

Open BrennanConroy opened this issue 2 years ago • 0 comments

Description

IHubClients and IHubCallerClients now hide interface members IClientProxy Client(string connectionId); and IClientProxy Caller { get; } with ISingleClientProxy Client(string connectionId); and ISingleClientProxy Caller { get; } in order to add support for client results.

This is not a breaking change to production code, unless you use reflection to call the above Client or Caller methods.

Version

.NET 7

Previous behavior

When using a testing library like Moq to unit test a SignalR Hub, you may write some code like follows:

var hub = new MyHub();
var mockCaller = new Mock<IHubCallerClients>();
var mockClientProxy = new Mock<IClientProxy>();
mockCaller.Setup(x => x.Caller).Returns(mockClientProxy.Object);
hub.Clients = mockCaller.Object;

class MyHub : Hub { }

New behavior

var hub = new MyHub();
var mockCaller = new Mock<IHubCallerClients>();
var mockClientProxy = new Mock<ISingleClientProxy>(); // <-- updated code
mockCaller.Setup(x => x.Caller).Returns(mockClientProxy.Object);
hub.Clients = mockCaller.Object;

class MyHub : Hub { }

Type of breaking change

  • [ ] Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • [X] Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.
  • [ ] Behavioral change: Existing code and binaries may experience different run-time behavior.

Reason for change

The change was made to add new functionality to SignalR and is non-breaking in normal use cases. The main area we see the change breaking is in test code which is easily updated.

Recommended action

Update test code to use the ISingleClientProxy interface when using reflection/reflection-based test code.

Affected APIs

IHubClients and IHubCallerClients

BrennanConroy avatar Jun 21 '22 16:06 BrennanConroy