How do I add Group support to a mocked Client in a SignalR 2.x unit testing framework?

纵然是瞬间 提交于 2019-12-01 06:01:35

Check this: 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();
        }

You could refer to this tutorial by the SingalR team.

If you want to check that a certain group was notified you should be able to do something like this (this is 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);
}

The code above is checking that my NotificationsHub.RaiseAlert(string groupId) method is only triggered on the client side for the specific groupId I pass in.

This is the hub that's being tested:

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("");

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!