是否可以使用集成测试的一部分SignalR
(https://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();
答案 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();
})