Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core Blazor服务器页面单元测试-如何模拟Blazor服务器页面中使用的辅助信号器客户端连接_Asp.net Core_Blazor_Blazor Server Side_Asp.net Blazor_Bunit - Fatal编程技术网

Asp.net core Blazor服务器页面单元测试-如何模拟Blazor服务器页面中使用的辅助信号器客户端连接

Asp.net core Blazor服务器页面单元测试-如何模拟Blazor服务器页面中使用的辅助信号器客户端连接,asp.net-core,blazor,blazor-server-side,asp.net-blazor,bunit,Asp.net Core,Blazor,Blazor Server Side,Asp.net Blazor,Bunit,我是bUnit的新用户,已经成功地为NavMenu运行了一些测试,以掌握基本概念。 但是,另一个Blazor页面向辅助信号器中心发出请求,以通信工作流状态 如何模拟信号机连接? 使用附加信号器连接来传达工作流状态的服务器页面 protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) {

我是bUnit的新用户,已经成功地为
NavMenu
运行了一些测试,以掌握基本概念。 但是,另一个Blazor页面向辅助信号器中心发出请求,以通信工作流状态

如何模拟信号机连接?

使用附加信号器连接来传达工作流状态的服务器页面

 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);
                }
            }
        }
AfterRenderAsync(bool firstRender)上的受保护重写异步任务
{
if(firstRender)
{
var hubUrl=NavigationManager.BaseUri.TrimEnd('/')+“/motionhub”;
尝试
{
Logger.LogInformation(“Index.razor页面正在执行初始渲染,连接到辅助信号器集线器”);
hubConnection=新的HubConnectionBuilder()
.WithUrl(hubUrl)
.ConfigureLogging(日志=>
{
logging.AddConsole();
logging.AddFilter(“Microsoft.AspNetCore.signal”,LogLevel.Information);
})
.AddJsonProtocol(选项=>
{
options.PayloadSerializerOptions=JsonConvertersFactory.CreateDefaultJsonConverters(LoggerMotionDetection、LoggerMotionInfo、LoggerJsonVisitor);
})
.Build();
hubConnection.On(“ReceiveMotionDetection”,ReceiveMessage);
hubConnection.Closed+=CloseHandler;
Logger.登录信息(“启动轮毂连接”);
等待hubConnection.StartAsync();
Logger.LogInformation(“已初始化索引页面,在signalR hub上侦听=>”+hubUrl.ToString());
}
捕获(例外e)
{
Logger.LogError(e,“遇到异常=>”+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}");
        }
    }
public类IndexTest:TestContext,IDisposable
{
私有存储库回购;
公共索引()
{
var mock=new mock();
repo=newMotionDetectionRepository(mock.Object,new NullLogger());
Services.addScope();
Services.addScope();
Services.addScope();
Services.addScope();
Services.addScope();
服务。AddScoped(sp=>repo);
Services.addScope();
Services.AddSignalR();
}
[事实]
公开无效测试()
{
var cut=RenderComponent();
Console.WriteLine($“{cut.Markup}”);
}
}
我使用这些代理

public interface IHubConnectionsBroker
{
    HubConnection CreateHubConnection(string endpoint, bool autoReconnect);
}

public interface IChatHubBroker
{
    event EventHandler<ChatMessage> OnReceiveMessage;
    event EventHandler<Exception> OnConnectionClosed;
    event EventHandler<Exception> OnReconnecting;
    event EventHandler<string> OnReconnected;

    bool IsConnected { get; }
    string ConnectionId { get; }
    TimeSpan HandshakeTimeout { get; set; }
    TimeSpan KeepAliveInterval { get; set; }
    TimeSpan ServerTimeout { get; set; }
    ChatHubBrokerState ChatHubBrokerState { get; }
    ValueTask StartAsync();
    ValueTask StopAsync();
    ValueTask SendAsync(ChatMessage message);
    ValueTask DisposeAsync();
}

公共接口IHubConnectionsBroker
{
HubConnection创建HubConnection(字符串端点,bool autoReconnect);
}
公共接口iCathubBroker
{
事件处理程序OnReceiveMessage;
事件处理程序OnConnectionClosed;
重新连接时的事件处理程序;
事件处理程序已重新连接;
布尔断开连接{get;}
字符串连接ID{get;}
TimeSpan握手超时{get;set;}
TimeSpan KeepAliveInterval{get;set;}
TimeSpan服务器超时{get;set;}
ChatHubBrokerState ChatHubBrokerState{get;}
ValueTask StartAsync();
ValueTask StopAsync();
ValueTask SendAsync(聊天信息);
ValueTask DisposeAsync();
}

干杯。是的,这足以让我开始,谢谢,感谢:)看起来微软的官方立场是使用包装器界面也看到了