是否可以使用作为集成测试的一部分生成的
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();
看起来
TestServer
需要您使用它创建的HttpMessageHandler
,所以将您的代码更改为
builder.WithUrl(new Uri(server.BaseAddress, "ws"), o =>
{
o.HttpMessageHandlerFactory = _ => server.CreateHandler();
})
应该可以。我认为明显错误的唯一一件事是您仅将测试主机地址传递给
WithUrl
,而它应该是到您的集线器的完整路径,即类似:
.WithUrl(new Uri(server.BaseAddress, Consts.TethysWebSocketPath))