SignalR 的 AspNet 核心集成测试

问题描述 投票:0回答:2

是否可以使用作为集成测试的一部分生成的

SignalR
来测试
TestServer
https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests view=aspnetcore-2.1 )?

我的所有重试均未能与

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();
asp.net-core signalr integration-testing
2个回答
5
投票

看起来

TestServer
需要您使用它创建的
HttpMessageHandler
,所以将您的代码更改为

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

0
投票

应该可以。我认为明显错误的唯一一件事是您仅将测试主机地址传递给

WithUrl
,而它应该是到您的集线器的完整路径,即类似:

.WithUrl(new Uri(server.BaseAddress, Consts.TethysWebSocketPath))
© www.soinside.com 2019 - 2024. All rights reserved.