【问题标题】:How do I add Group support to a mocked Client in a SignalR 2.x unit testing framework?如何在 SignalR 2.x 单元测试框架中向模拟客户端添加组支持?
【发布时间】:2014-05-03 00:25:36
【问题描述】:

我正在使用 Moq 为我的 SignalR 2.x 应用程序构建 UnitTest 框架。 我目前正在通过以下方式模拟我的客户:

var mockClients = new Mock<IHubCallerConnectionContext>();
Clients = mockClients.Object;

为了测试,我需要测试按组发送消息:

Clients.Group(groupName).sendSomeMessage(message);

如何向我的模拟客户端添加组支持?

【问题讨论】:

    标签: c# signalr moq signalr-hub signalr.client


    【解决方案1】:

    检查这个: https://github.com/SignalR/SignalR/blob/release/tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs

     public void HubsGroupAreMockable()
            {
                var hub = new MyTestableHub();
                var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
                var groups = new Mock<IClientContract>();
    
                hub.Clients = mockClients.Object;
                groups.Setup(m => m.send(It.IsAny<string>())).Verifiable();
                mockClients.Setup(m => m.Group("test")).Returns(groups.Object);
                hub.SendGroup("test", "foo");
    
                groups.VerifyAll();
            }
    

    【讨论】:

      【解决方案2】:

      您可以参考 SingalR 团队的 this 教程。

      【讨论】:

      • 这是我最初开始使用的示例。它真的是为示例信号器聊天应用程序定制的。这是一个好的开始,但它并没有深入到我需要的细节。消息被广播到“所有”客户端。我需要模拟组对象,以便在单元测试中向特定组的成员广播。 (请参考上面的示例代码)。谁能帮我指出如何模拟 Group 对象的方向? JB
      • 你找到这个问题的答案了吗?
      【解决方案3】:

      如果您想检查某个组是否已收到通知,您应该可以执行以下操作(这是 SignalR 2):

      public interface IClientContract
      {
          void RaiseAlert(string message);
      }
      
      [Test]
      public void NotifiesOnlySpecifiedGroupWhenGroupIdSent()
      {
          // Adapted from code here: https://github.com/SignalR/SignalR/blob/dev/tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs
      
          // Arrange
      
          // Set up the individual mock clients
          var group1 = new Mock<IClientContract>();
          var group2 = new Mock<IClientContract>();
          var group3 = new Mock<IClientContract>();
      
          group1.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();
          group2.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();
          group3.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();
      
          // set the Connection Context to return the mock clients
          var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
          mockClients.Setup(m => m.Group("1")).Returns(group1.Object);
          mockClients.Setup(m => m.Group("2")).Returns(group2.Object);
          mockClients.Setup(m => m.Group("3")).Returns(group3.Object);
      
          // Assign our mock context to our hub
          var hub = new NotificationsHub
          {
              Clients = mockClients.Object
          };
      
          // Act
          hub.RaiseNotificationAlert("2");
      
          // Assert
          group1.Verify(m => m.RaiseAlert(It.IsAny<string>()), Times.Never);
          group2.Verify(m => m.RaiseAlert(""), Times.Once);
          group3.Verify(m => m.RaiseAlert(It.IsAny<string>()), Times.Never);
      }
      

      上面的代码正在检查我的NotificationsHub.RaiseAlert(string groupId) 方法是否仅在客户端针对我传入的特定groupId 触发。

      这是正在测试的中心:

      public class NotificationsHub : Hub
      {
          public void RaiseAlert(string message)
          {
              Clients.All.RaiseAlert(message);
          }
      
          public void RaiseNotificationAlert(string groupId)
          {
              if (groupId== null)
              {
                  // Notify all clients
                  Clients.All.RaiseAlert("");
                  return;
              }
      
              // Notify only the client for this userId
              Clients.Group(groupId).RaiseAlert("");
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 2017-09-26
        • 1970-01-01
        • 2016-03-27
        • 2018-09-07
        • 1970-01-01
        相关资源
        最近更新 更多