【问题标题】:Mocking a Context.ConnectionId for Unit Testing Signalr .NetCore Messaging Hubs为单元测试 Signalr .NetCore 消息集线器模拟 Context.ConnectionId
【发布时间】:2019-07-03 14:05:44
【问题描述】:

我的问题是,当我在 .Net CORE 中对 Signalr 集线器进行单元测试时,获取 context.connection ID 值以插入我的方法之一。我的方法在我的测试类中如下所示:


    [Fact]
    public async Task TestWorkstationCreation()
    {
        Mock<IHubCallerClients<IWorkstation>> mockClients = new Mock<IHubCallerClients<IWorkstation>>();
        Mock<IWorkstation> mockClientProxy = new Mock<IWorkstation>();
        mockClients.Setup(clients => clients.All).Returns(mockClientProxy.Object);
        _workstationHub.Clients = mockClients.Object;
        await _workstationHub.RegisterWorkstation("WKS16", "Ready", new Dictionary<string, string> {{"OS", "Windows 10"}, {"Exam", "GRE, TOEFL"}});
        mockClientProxy.Verify(c => c.WorkstationRegistered(It.IsAny<WorkstationDataModel>(), It.IsAny<string>()), Times.AtLeastOnce);
    }

在我的 hub 类中,方法如下:

    public async Task RegisterWorkstation(string id, string status, Dictionary<string, string> capabilities)
    {
        _logger.LogInformation(
            "Registering a Workstation with id: {id}, status: {status}, and capabilities: {capabilities}",
            id, status, string.Join(",", capabilities));
        var workstationAdded = AddWorkstation(id, status, capabilities, Context.ConnectionId);
        var message = workstationAdded == null
            ? $"A workstation with the id: {id} already exists!"
            : $"A workstation with the id: {id}, status: {status}, and capabilities: {string.Join(",", capabilities)} " +
              "was added to the current list of workstations available.";
        await Clients.All.WorkstationRegistered(workstationAdded, message);
    }

测试时,在 Context.ConnectionId 处抛出对象引用未设置空指针异常。有没有办法模拟一个可以使用的 Context.Connection Id?

【问题讨论】:

    标签: asp.net-core .net-core signalr xunit asp.net-core-signalr


    【解决方案1】:

    我最终为单元测试做了这件事,并且成功了

            [Fact]
            public async Task TestWorkstationCreation()
            {
                Mock<IHubCallerClients<IWorkstation>> mockClients = new Mock<IHubCallerClients<IWorkstation>>();
                Mock<IWorkstation> mockClientProxy = new Mock<IWorkstation>();
                Mock<HubCallerContext> mockClientContext = new Mock<HubCallerContext>();
                mockClients.Setup(clients => clients.All).Returns(mockClientProxy.Object);
                mockClientContext.Setup(c => c.ConnectionId).Returns(Guid.NewGuid().ToString);
                _workstationHub.Clients = mockClients.Object;
                _workstationHub.Context = mockClientContext.Object;
                await _workstationHub.RegisterWorkstation("WKS16", "Ready", new Dictionary<string, string> {{"OS", "Windows 10"}, {"Exam", "GRE, TOEFL"}});
                mockClientProxy.Verify(c => c.WorkstationRegistered(It.IsAny<WorkstationDataModel>(), It.IsAny<string>()), Times.AtLeastOnce);
            }
    

    【讨论】:

      猜你喜欢
      • 2012-06-03
      • 1970-01-01
      • 2020-03-28
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多