在单元测试中模拟SignalR Clients.Group的方法 [英] Ways to mock SignalR Clients.Group in unit test

查看:510
本文介绍了在单元测试中模拟SignalR Clients.Group的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为SignalR应用程序编写模拟测试用例.我只是借助

I'm writing mock test cases for SignalR application. I just started with the help of Unit Testing SignalR Applications, but my requirement is little bit different that example shown there. Following is the code I have done after googling.

public class HubServer : Hub
{
    [HubMethodName("SendNofication")]
    public void GetNofication(string message)
    {
        try
        {
            Clients.Group("Manager").BroadCast(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }        
}

单元测试

public interface IClientContract { }

[TestMethod]
public void GetNoficationTest()
{
    var hub = new HubServer();
    var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
    hub.Clients = mockClients.Object;
    var all = new Mock<IClientContract>();
    string message = "Message to send";

    mockClients.Setup(m => m.Group("Manager")).Returns(message);
    mockClients.Setup(m => m.All).Returns(all.Object);

    hub.GetNofication("Mock call to SingalR hub");
    all.VerifyAll();
}

我是单元测试应用程序的初学者,只想确认这是否是模拟SignalR组的正确方法.

I'm beginner in Unit Test application, just want to confirm if this is right way to mock SignalR Groups.

推荐答案

使用集线器组可模拟

客户合同需要使用必要的方法进行更新.在这种情况下,您将必须添加Broadcast方法以匹配在集线器中的调用方式

The client contract needs to be updated with the necessary methods. In this case you will have to add Broadcast method to match how it is called in the Hub

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

下一步,通过为集线器设置依赖项来安排测试,并练习被测方法.

Next arrange the test by setting up the dependencies for the hub and exercise the method under test.

[TestMethod]
public void _GetNoficationTest() {
    // Arrange.
    var hub = new HubServer();
    var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
    var groups = new Mock<IClientContract>();
    var message = "Message to send";
    var groupName = "Manager";

    hub.Clients = mockClients.Object;
    groups.Setup(_ => _.Broadcast(message)).Verifiable();
    mockClients.Setup(_ => _.Group(groupName)).Returns(groups.Object);

    // Act.
    hub.GetNofication(message);

    // Assert.
    groups.VerifyAll();
}

另一种选择是使用ExpandoObject伪造该组,因为在这种情况下Clients.Group返回dynamic

Another option would be to fake the group using an ExpandoObject given that Clients.Group in this instance returns dynamic

[TestMethod]
public void _GetNoficationTest_Expando() {
    // Act.
    var hub = new HubServer();
    var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
    dynamic groups = new ExpandoObject();
    var expected = "Message to send";
    string actual = null;
    var groupName = "Manager";
    bool messageSent = false;

    hub.Clients = mockClients.Object;
    groups.Broadcast = new Action<string>(m => {
        actual = m;
        messageSent = true;
    });
    mockClients.Setup(_ => _.Group(groupName)).Returns((ExpandoObject)groups);

    // Act.
    hub.GetNofication(expected);

    // Assert.
    Assert.IsTrue(messageSent);
    Assert.AreEqual(expected, actual);
}

这篇关于在单元测试中模拟SignalR Clients.Group的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆