SIGNALR长轮询测试在Debug WebAPI集成单元测试上进行跑步通行

问题描述 投票:0回答:0

WebApiTestFactory Configure Implementation Web Notification Provider Mapping Notification Hub

当_configure?invoke(builder)时,useendpoint maphub被调用。

当测试运行时,我们会看到以下错误:

System.InvalidoperationException:端点/集线器/通知/协商包含授权元数据,但是没有发现中间件支持授权。 通过在应用程序启动代码中添加app.useauthorization()来配置应用程序启动。如果有呼叫app.userouting()和app.useendpoints(...),则必须在它们之间进行调用。

当我们等待连接startasync时,这会发生,但是,如果我们调试测试,一切都可以正常工作,并且测试通过。

这是单位测试:

public async Task CanConnectClientHubToServerUsingLongPolling() { ConfigureServicesForTest((ctx, services) => { services.AddNotification(builder => { builder.Configure(options => { options.Enabled = true; }); builder.AddWebNotification(web => { web.AddHubOptions(hub => { }); }); }); }); Configure(app => { app.UseNotificationServices(); }); INotifier notifier = null!; IAuthenticationTokensAccessor accessor = null!; FluentActions.Invoking(() => { notifier = Services.GetRequiredService<INotifier>(); accessor = Services.GetRequiredService<IAuthenticationTokensAccessor>(); }).Should().NotThrow(); var conn = CreateConnectionUsingLongPolling( Services.GetRequiredService<IOptionsMonitor<WebNotificationOptions>>().Get(Options.DefaultName), async () => (await accessor.GetAuthenticationTokensAsync("bob")).EncodedUserJwt); string expected = "this is a test"; string recieved = ""; conn.On<string>(HubMethods.RecieveSystemMessage, (msg) => { Console.WriteLine($"System: {msg}"); }); conn.On<string>(HubMethods.RecieveMessage, (msg) => { recieved = msg; _delayForMessageSentTokenSource.Cancel(); }); await conn.StartAsync(); conn.ConnectionId.Should().NotBeNull(); conn.State.Should().Be(HubConnectionState.Connected); await notifier.BroadCastAsync(expected); DelayAsync(TimeSpan.FromSeconds(60)); recieved.Should().Be(expected); await conn.StopAsync(); }

几天来,我一直在尝试解决此问题,但是它是一个头部刮擦者,我可以使用一些帮助。 webapplicationFactory使用的程序入口点是此处

using SubSonic.Configuration; using SubSonic.Notification; var builder = WebApplication.CreateBuilder(args); if(!builder.Environment.IsTestIntegration()) { var section = builder.Configuration.GetRequiredSection("Settings"); builder.Services.AddEnvironment(opt => { opt.Environment = section.GetRequiredValue<string>("Environment"); }); builder.Services .AddHttpContextAccessor() .AddControllers(); builder.Services.AddNotification(notify => { notify.Configure(options => { options.Enabled = true; }); notify.AddWebNotification(web => { }); }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); } // program is primarily hosted in a test framework for integration testing of packages var app = builder.Build(); if (!app.Environment.IsTestIntegration()) { if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseNotificationServices(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); } app.Run();
没有调试的日志运行:

dbug:microsoft.extensions.hosting.internal.host

1

托管开始
dbug:subsonic.testing.webapitestfactory [0]
调用用户outing

dbug:subsonic.testing.webapitestfactory [0] 端点路由中间件添加了false

dbug:subsonic.testing.webapitestfactory [0] Builder属性:[application.services,microsoft.extensions.dependentienctive.servicelookup.serviceproviderenginescope]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[server.features,microsoft.aspnetcore.http.features.featurecollection]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__ENDPOINTROUTEBUILDER,MICROSOFT.ASPNETCORE.ROUTING.DEFAULTENDENDPOINTROUTEBUILDER]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__userouting,system.func`2 [microsoft.aspnetcore.builder.iapplicationbuilder,microsoft.aspnetcore.builder.iapplicationbuilder]]

烧伤:subsonic.notification.notification providerFactory [0] 映射到 /枢纽 /通知

的NotificationHub

dbug:microsoft.extensions.hosting.internal.host

2

托管开始

在此处运行的日志:

dbug:microsoft.extensions.hosting.internal.host

1 托管开始

dbug:subsonic.testing.webapitestfactory [0] 调用用户outing

dbug:subsonic.testing.webapitestfactory [0] 端点路由中间件添加了true

dbug:subsonic.testing.webapitestfactory [0] Builder属性:[application.services,microsoft.extensions.dependentienctive.servicelookup.serviceproviderenginescope]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[server.features,microsoft.aspnetcore.http.features.featurecollection]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__MiddleWaredScriptions,System.Collections.generic.list'1 [System.String]]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__ENDPOINTROUTEBUILDER,MICROSOFT.ASPNETCORE.ROUTING.DEFAULTENDENDPOINTROUTEBUILDER]

dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__userouting,system.func`2 [microsoft.aspnetcore.builder.iapplicationbuilder,microsoft.aspnetcore.builder.iapplicationbuilder]]

dbug:subsonic.testing.webapitestfactory [0] microsoft.aspnetcore.hostfiltering.hostfilteringmiddleware

dbug:subsonic.testing.webapitestfactory [0] microsoft.aspnetcore.httpspolicy.httpsredirectionmiddleware

dbug:subsonic.testing.webapitestfactory [0] Microsoft.aspnetcore.Routing.EndPoIntRoutingMiddleware

烧伤:subsonic.notification.notification providerFactory [0] 映射到 /枢纽 /通知

的NotificationHub

dbug:microsoft.extensions.hosting.internal.host

2

托管开始

根据错误消息,您丢失了the the Middleware。

您可以像下面一样cahnge您的代码。

app.UseAuthentication();

unit-testing asp.net-core signalr webapplicationfactory
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.