Skip to content

Commit 866fe9f

Browse files
committed
Make Hub<T>.Clients settable for testing
- Setting Hub<T>.Clients to null will really set it to its default value SignalR#3299
1 parent 8d315ac commit 866fe9f

File tree

2 files changed

+25
-1
lines changed
  • src/Microsoft.AspNet.SignalR.Core
  • tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs

2 files changed

+25
-1
lines changed

src/Microsoft.AspNet.SignalR.Core/Hub`T.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ namespace Microsoft.AspNet.SignalR
99
/// </summary>
1010
public abstract class Hub<T> : Hub where T : class
1111
{
12+
// This allows the typed Client property to be settable for testing
13+
private IHubCallerConnectionContext<T> _testClients;
14+
1215
/// <summary>
1316
/// Gets a dynamic object that represents all clients connected to this hub (not hub instance).
1417
/// </summary>
1518
public new IHubCallerConnectionContext<T> Clients
1619
{
1720
get
1821
{
19-
return new TypedHubCallerConnectionContext<T>(base.Clients);
22+
return _testClients ?? new TypedHubCallerConnectionContext<T>(base.Clients);
23+
}
24+
set
25+
{
26+
_testClients = value;
2027
}
2128
}
2229
}

tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ public void TypedHubsCanExplicitelyImplementIHub()
134134
all.VerifyAll();
135135
}
136136

137+
[Fact]
138+
public void TypedIHubCallerConnectionContextIsSettable()
139+
{
140+
// https://github.com/SignalR/SignalR/issues/3299
141+
var mockClients = new Mock<IHubCallerConnectionContext<IClientContract>>();
142+
var all = new Mock<IClientContract>();
143+
all.Setup(m => m.send("foo"));
144+
mockClients.Setup(m => m.All).Returns(all.Object);
145+
146+
var hub = new MyTypedIHub();
147+
hub.Clients = mockClients.Object;
148+
hub.Send("foo");
149+
150+
mockClients.VerifyAll();
151+
all.VerifyAll();
152+
}
153+
137154
private class MyTestableHub : Hub
138155
{
139156
public void Send(string messages)

0 commit comments

Comments
 (0)