Signalr C#客户端在单元测试(SpecFlow)中执行时未接收数据

发布于 2025-01-21 13:29:18 字数 2004 浏览 0 评论 0原文

我正在尝试使用SpecFlow对应用程序进行端到端测试。正在测试的系统是某种票务系统。可以通过调用Web API来创建新的票证,并且在创建新票时,使用SignalR发送了通知。 SpecFlow方案看起来像是

@Notifications
Scenario: A notification is sent when a new ticket has been created
    Given I am receiving change notifications when a new ticket has been created
    When I create a new ticket
    Then the new ticket is sent by a push notification

以下方法:

//Step 1
[Given(@"I am receiving change notifications when a new ticket has been created")]
public async Task GivenIAmReceivingChangeNotificationsWhenANewTicketHasBeenCreated()
{    
    var connection = new HubConnectionBuilder()
    .WithUrl("https://localhost:5001/ws/v1/ticket")
    .AddJsonProtocol(o => o.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter()))
    .ConfigureLogging(x =>
    {
        //Custom ILogger which does nothing but enables me to set breakpoints
        x.AddProvider(new DoNothingLoggerProvider());
    })
    .Build();

    await connection.StartAsync(CancellationToken.None);

    connection.On<TicketDetails>("NewTicketRegistered", ticket =>
    {
      throw new NotImplementedException("Never reached")
    });
}

//Step 2
[When(@"I create a new ticket")]
public async Task WhenICreateANewTicket()
{        
    var ticketId = await MyApiClient.TicketsPostAsync(new NewTicket(), CancellationToken.None);
    ticketId.Should().NotBe(default(Guid));
}

//Step 3
[Then(@"the new ticket is sent by a push notification")]
public async Task ThenTheNewTicketIsSentByAPushNotification()
{   
   await Task.Delay(5000);
}

此处,TicketDetailsMyapiclient是从OpenAPI生成的。

我的问题是,在connection.on&lt; ticketdetails&gt;中列出的回调永远不会被调用,SpecFlow将在第2步之后(呼吁API创建新的票),继续步骤3,等待5在测试方案完成之前。在这5秒钟内,已发送了SignalR的通知。我已经验证了创建一个超简单的控制台应用程序,该应用程序与步骤1相同,只是它将消息写入控制台而不是抛出异常。当我在执行SpecFlow测试时运行该控制台应用程序时,我看到数据是在Specflow前进到第3步的同时发送到控制台的。这告诉我,设置连接的代码工作起作用,并且通知是在SpecFlow测试完成之前发送。

我在这里想念什么?

I'm trying to do an end-to-end testing of an application using SpecFlow. The system under test is some kind of ticketing system; a new ticket can (only) be created by calling a Web API, and when a new ticket has been created, a notification is sent using SignalR. The SpecFlow scenario looks like

@Notifications
Scenario: A notification is sent when a new ticket has been created
    Given I am receiving change notifications when a new ticket has been created
    When I create a new ticket
    Then the new ticket is sent by a push notification

which maps to the following methods:

//Step 1
[Given(@"I am receiving change notifications when a new ticket has been created")]
public async Task GivenIAmReceivingChangeNotificationsWhenANewTicketHasBeenCreated()
{    
    var connection = new HubConnectionBuilder()
    .WithUrl("https://localhost:5001/ws/v1/ticket")
    .AddJsonProtocol(o => o.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter()))
    .ConfigureLogging(x =>
    {
        //Custom ILogger which does nothing but enables me to set breakpoints
        x.AddProvider(new DoNothingLoggerProvider());
    })
    .Build();

    await connection.StartAsync(CancellationToken.None);

    connection.On<TicketDetails>("NewTicketRegistered", ticket =>
    {
      throw new NotImplementedException("Never reached")
    });
}

//Step 2
[When(@"I create a new ticket")]
public async Task WhenICreateANewTicket()
{        
    var ticketId = await MyApiClient.TicketsPostAsync(new NewTicket(), CancellationToken.None);
    ticketId.Should().NotBe(default(Guid));
}

//Step 3
[Then(@"the new ticket is sent by a push notification")]
public async Task ThenTheNewTicketIsSentByAPushNotification()
{   
   await Task.Delay(5000);
}

Here, TicketDetails and MyApiClient are generated from OpenAPI.

My problem is that the callback listed in connection.On<TicketDetails> is never invoked, SpecFlow will after step 2 (API is called to create a new ticket) go on to step 3 where it waits for 5 sec before the test scenario completes. During these 5 seconds, the notification from SignalR has been sent. I have verified that by created a super simple console application, which does the same as step 1, except it writes a message to the console instead of throwing an exception. When I run that console app while executing the SpecFlow test, I see that data is sent to the console at the same time as SpecFlow advances to step 3. This tells me that the code for setting up the connection works, and that the notifications are sent before the SpecFlow test completes.

What am I missing here?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文