forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHubControllerBaseTest.cs
More file actions
108 lines (91 loc) · 4.17 KB
/
Copy pathHubControllerBaseTest.cs
File metadata and controls
108 lines (91 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Infrastructure;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http
{
public class HubControllerBaseTest
{
[Fact]
public void Clients_ThrowsInvalidOperationForNullHubContext()
{
Assert.Throws<InvalidOperationException>(
() => new HubContextController(null).Clients,
"No hub context could be found for the HubController of type 'HubContextController'.");
}
[Fact]
public void Clients_ReturnsHubContextsClients()
{
Mock<IHubContext> mockContext = new Mock<IHubContext>();
IHubConnectionContext clients = new HubConnectionContext();
mockContext.Setup(mock => mock.Clients).Returns(clients);
var controller = new HubContextController(mockContext.Object);
Assert.Same(clients, controller.Clients);
}
[Fact]
public void Groups_ThrowsInvalidOperationForNullHubContext()
{
Assert.Throws<InvalidOperationException>(
() => new HubContextController(null).Groups,
"No hub context could be found for the HubController of type 'HubContextController'.");
}
[Fact]
public void Groups_ReturnsHubContextsClients()
{
Mock<IHubContext> mockContext = new Mock<IHubContext>();
IGroupManager groups = new Mock<IGroupManager>().Object;
mockContext.Setup(mock => mock.Groups).Returns(groups);
var controller = new HubContextController(mockContext.Object);
Assert.Same(groups, controller.Groups);
}
[Fact]
public void ConnectionManager_ReturnsGlobalConnectionManager_IfConfigurationIsNull()
{
HubContextController controller = new HubContextController();
Assert.Same(GlobalHost.ConnectionManager, controller.GetConnectionManager());
}
[Fact]
public void ConnectionManager_ReturnsGlobalConnectionManager_IfCannotResolveIConnectionManager()
{
HubContextController controller = new HubContextController();
controller.Configuration = new HttpConfiguration();
Mock<System.Web.Http.Dependencies.IDependencyResolver> mockDependencyResolver = new Mock<Dependencies.IDependencyResolver>();
mockDependencyResolver.Setup(mock => mock.GetService(typeof(IConnectionManager))).Returns(null);
controller.Configuration.DependencyResolver = mockDependencyResolver.Object;
Assert.Same(GlobalHost.ConnectionManager, controller.GetConnectionManager());
}
[Fact]
public void ConnectionManager_ReturnsConnectionManagerFromDependencyResolver_IfFound()
{
HubContextController controller = new HubContextController();
controller.Configuration = new HttpConfiguration();
IConnectionManager connectionManager = new Mock<IConnectionManager>().Object;
Mock<System.Web.Http.Dependencies.IDependencyResolver> mockDependencyResolver = new Mock<Dependencies.IDependencyResolver>();
mockDependencyResolver.Setup(mock => mock.GetService(typeof(IConnectionManager))).Returns(connectionManager);
controller.Configuration.DependencyResolver = mockDependencyResolver.Object;
Assert.Same(connectionManager, controller.GetConnectionManager());
}
public class HubContextController : HubControllerBase
{
IHubContext _hubContext;
public HubContextController(IHubContext hubContext = null)
{
_hubContext = hubContext;
}
protected override IHubContext HubContext
{
get
{
return _hubContext;
}
}
public IConnectionManager GetConnectionManager()
{
return ConnectionManager;
}
}
}
}