我是 bUnit 的新用户,并设法运行了一些测试NavMenu
以掌握基本概念。但是,不同的 Blazor 页面向辅助 signalR 集线器发出请求以进行工作流状态的通信。
如何模拟signalR连接? https://github.com/dotnet/aspnetcore/issues/14924
使用附加 signalR 连接来传达工作流状态的服务器页面
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
var hubUrl = NavigationManager.BaseUri.TrimEnd('/') + "/motionhub";
try
{
Logger.LogInformation("Index.razor page is performing initial render, connecting to secondary signalR hub");
hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl)
.ConfigureLogging(logging =>
{
logging.AddConsole();
logging.AddFilter("Microsoft.AspNetCore.SignalR", LogLevel.Information);
})
.AddJsonProtocol(options =>
{
options.PayloadSerializerOptions = JsonConvertersFactory.CreateDefaultJsonConverters(LoggerMotionDetection, LoggerMotionInfo, LoggerJsonVisitor);
})
.Build();
hubConnection.On<MotionDetection>("ReceiveMotionDetection", ReceiveMessage);
hubConnection.Closed += CloseHandler;
Logger.LogInformation("Starting HubConnection");
await hubConnection.StartAsync();
Logger.LogInformation("Index Razor Page initialised, listening on signalR hub => " + hubUrl.ToString());
}
catch (Exception e)
{
Logger.LogError(e, "Encountered exception => " + e);
}
}
}
存根单元测试类
public class IndexTest : TestContext, IDisposable
{
private MotionDetectionRepository repo;
public IndexTest()
{
var mock = new Mock<IMotionDetectionSerializer<MotionDetection>>();
repo = new MotionDetectionRepository(mock.Object, new NullLogger<MotionDetectionRepository>());
Services.AddScoped<ILogger<MotionDetectionRepository>, NullLogger<MotionDetectionRepository>>();
Services.AddScoped<ILogger<MotionInfoConverter>, NullLogger<MotionInfoConverter>>();
Services.AddScoped<ILogger<MotionDetectionConverter>, NullLogger<MotionDetectionConverter>>();
Services.AddScoped<ILogger<JsonVisitor>, NullLogger<JsonVisitor>>();
Services.AddScoped<ILogger<WebApp.Pages.Index>, NullLogger<WebApp.Pages.Index>>();
Services.AddScoped<IMotionDetectionRepository>(sp => repo);
Services.AddScoped<MockNavigationManager>();
Services.AddSignalR();
}
[Fact]
public void Test()
{
var cut = RenderComponent<WebApp.Pages.Index>();
Console.WriteLine($"{cut.Markup}");
}
}