用于SignalR的AspNet核心集成测试

时间:2018-07-10 12:43:39

标签: asp.net-core signalr integration-testing

是否可以使用集成测试的一部分SignalRhttps://docs.microsoft.com/en-us/aspnet/core/test/integration-tests view = aspnetcore-2.1)测试TestServer

我所有的重试均无法执行到TestServer实例的握手,但失败,并出现异常:System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it

Debug / Console / IIS中正常运行

服务器代码

 public void Configure(...)
 {
     ...
     app.UseSignalR(router => router.MapHub<MockHub>("/ws"));
     ...
}

测试代码

        ...
        var server = factory.Server;
        var connection = new HubConnectionBuilder()
              .WithUrl(server.BaseAddress)
              .Build();
        await connection.StartAsync();

2 个答案:

答案 0 :(得分:0)

应该可以。我唯一看到的明显错误是您只将测试主机地址传递到WithUrl,而它应该是集线器的完整路径,即:

.WithUrl(new Uri(server.BaseAddress, Consts.TethysWebSocketPath))

答案 1 :(得分:0)

看起来像TestServer要求您使用它创建的HttpMessageHandler,因此将代码更改为

.WithUrl(new Uri(server.BaseAddress, "ws"), o => { o.HttpMessageHandlerFactory = _ => server.CreateHandler(); })