Ways to mock SignalR Clients.Group in unit test

Viewed 5293

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.

SignalRHub

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

Unit Test

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

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

1 Answers
Related