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.
Using examples from Microsoft.AspNet.SignalR.Tests
Hubs Group Are Mockable
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 HubNext arrange the test by setting up the dependencies for the hub and exercise the method under test.
Another option would be to fake the group using an
ExpandoObject
given thatClients.Group
in this instance returnsdynamic