【发布时间】:2017-11-24 11:01:45
【问题描述】:
我尝试为我的 Hub 方法编写测试,但我不知道,因为当前 (1.0.0-alpha2-final) 版本的 SignalR 没有文档或代码示例。有我的代码:
[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 在运行测试时生成下一条错误消息:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'Moq.Language.Flow.ISetup' 不包含“退货”的定义
在旧版本中,模拟客户端和组的创建如下所示:
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)
但是我现在不能使用 IHubCallerConnectionContext,所以我尝试了:
var mockClients = new Mock<IHubClients>();
但我不知道在这种情况下如何创建模拟组
对不起我糟糕的英语
【问题讨论】:
-
您的问题找到答案了吗?我也有同样的问题。
-
我也在寻找这个答案。
标签: c# unit-testing mocking asp.net-core-2.0 asp.net-core-signalr