如何测试SignalR 2集线器只通知单个用户?

时间:2017-10-26 11:47:27

标签: asp.net asp.net-mvc unit-testing signalr signalr-hub

我已经完成了unit testing examples in the SignalR 2 documentation,但现在我想测试一下,我的中心只在某些情况下通知单个用户。

我的集线器代码如下所示:

public class NotificationsHub : Hub
{
    public void RaiseAlert(string message)
    {
        Clients.All.RaiseAlert(message);
    }

    public void RaiseNotificationAlert(string userId)
    {
        if (userId == null)
        {
            // Notify all clients
            Clients.All.RaiseAlert("");
            return;
        }

        // Notify only the client for this userId
        Clients.User(userId).RaiseAlert("");

    }
}

我检查所有客户端都被通知的单元测试看起来像这样(它基于Microsoft的例子):

[Test]
public void NotifiesAllUsersWhenNoUserIdSpecified()
{
    // Based on: https://docs.microsoft.com/vi-vn/aspnet/signalr/overview/testing-and-debugging/unit-testing-signalr-applications

    // Arrange

    // This is faking the 
    var mockClients = new Mock<IClientContract>();
    mockClients.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();

    // A mock of our SignalR hub's clients 
    var mockClientConnCtx = new Mock<IHubCallerConnectionContext<dynamic>>();
    mockClientConnCtx.Setup(m => m.All).Returns(mockClients.Object);

    // Set the hub's connection context to the mock context
    var hub = new NotificationsHub
    {
        Clients = mockClientConnCtx.Object
    };

    // Action
    hub.RaiseNotificationAlert(null);

    // Assert
    mockClients.Verify(m => m.RaiseAlert(It.IsAny<string>()));
}

我不确定的是如何将var mockClients = new Mock<IClientContract>()行所代表的客户端集合更改为一组个人客户端,以便我可以测试如果我通知用户1,那么用户2和3没有得到通知?

1 个答案:

答案 0 :(得分:0)

我发现了另一个关于如何对测试组进行单元化的问题,one of the answers指向了unit tests for the SignalR codebase

看看我制定的这些示例,我需要添加对User mockClients方法的调用的模拟。结果看起来像这样:

public interface IClientContract
{
    void RaiseAlert(string message);
}

[Test]
public void NotifiesOnlySpecifiedUserWhenUserIdSent()
{
    // 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 client1 = new Mock<IClientContract>();
    var client2 = new Mock<IClientContract>();
    var client3 = new Mock<IClientContract>();

    client1.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();
    client2.Setup(m => m.RaiseAlert(It.IsAny<string>())).Verifiable();
    client3.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.User("1")).Returns(client1.Object);
    mockClients.Setup(m => m.User("2")).Returns(client2.Object);
    mockClients.Setup(m => m.User("3")).Returns(client3.Object);

    // Assign our mock context to our hub
    var hub = new NotificationsHub
    {
        Clients = mockClients.Object
    };

    // Act
    hub.RaiseNotificationAlert("2");

    // Assert
    client1.Verify(m => m.RaiseAlert(It.IsAny<string>()), Times.Never);
    client2.Verify(m=>m.RaiseAlert(""), Times.Once);
    client3.Verify(m => m.RaiseAlert(It.IsAny<string>()), Times.Never);
}