I try to write test for my Hub method, but I don't know as because there is no documentation or code examples for current (1.0.0-alpha2-final) version of SignalR. There is my code:
[Fact]
public void SaveVisitorInfoTest()
{
//Arrange
var chatHub = new ChatHub();
var mockClients = new Mock<IHubClients>();
chatHub.Clients = mockClients.Object;
dynamic groups = new ExpandoObject();
var groupName = "SomeConversation";
string actualName = null;
string expectedName = "someName";
groups.SendVisitorInfo = new Action<string, string>(n => {
actualName = n;
});
mockClients.Setup(_ => _.Group(groupName)).Returns(groups);
//Act
chatHub.Clients.Group(groupName).InvokeAsync("SendVisitorInfo", expectedName);
// Assert
Assert.Equal(expectedName, actualName);
}
Visual Studio generates next error message while running the test:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'Moq.Language.Flow.ISetup' does not contain a definition for 'Returns'
In old versions mock clients and groups creation looks like this:
var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
dynamic groups = new ExpandoObject();
groups.SendVisitorInfo = new Action<string, string>(n => {
actualName = n;
});
mockClients.Setup(_ => _.Group(groupName)).Returns((ExpandoObject)groups)
But I can't use IHubCallerConnectionContext now, so I tried:
var mockClients = new Mock<IHubClients>();
but i don't know how to create mock groups in this case
Sorry for my terrible english