是否可以从HubConnection
初始化Microsoft.AspNetCore.TestHost.TestServer
?
以下示例在HttpRequestException
await hubConnection.StartAsync();
(未找到)异常
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.TestHost;
using Xunit;
namespace FunctionalTests
{
public class PubSubScenarios
{
[Fact]
public async Task SignalRHubTest_Foo()
{
var webHostBuilder = WebHost.CreateDefaultBuilder().UseStartup<Startup>();
using (var testServer = new TestServer(webHostBuilder))
{
var hubConnection = await StartConnectionAsync(testServer.BaseAddress);
}
}
private static async Task<HubConnection> StartConnectionAsync(Uri baseUri)
{
var hubConnection = new HubConnectionBuilder()
.WithUrl($"http://{baseUri.Host}/fooHub")
.WithConsoleLogger()
.Build();
await hubConnection.StartAsync();
return hubConnection;
}
}
}
答案 0 :(得分:3)
您需要调用testServer.CreateHandler()并将HttpMessageHandler传递给WithMessageHandler:
HtmlWebpackPlugin
这对于websockets不起作用(我在这里打开了一个问题https://github.com/aspnet/SignalR/issues/1595