aspnet / Announcements

Subscribe to this repo to be notified about major changes in ASP.NET Core and Entity Framework Core
Other
1.66k stars 80 forks source link

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

Open BrennanConroy opened 2 years ago

BrennanConroy commented 2 years ago

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

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