Azure 服务总线主题触发器的 xUnit 测试

发布于 2025-01-11 23:36:51 字数 1016 浏览 4 评论 0原文

我是 xUnit 和 Azure 服务总线的新手。我找不到使用 xUnit 框架的单元测试示例。感谢您帮助我提供一些示例代码,这些代码展示了如何模拟和编写以下函数的测试?

    [Function("ReceiveMessageFromTopic")]
    public void Run([ServiceBusTrigger("myTopic", "myTopicSubscription", Connection = "ServiceBusConnection")] string triggeredMessage)
    {
        ExpectedMessage receivedMessage = new();
        _logger.LogInformation($"ServiceBus topic trigger function processed message: {triggeredMessage}");

        if (string.IsNullOrEmpty(triggeredMessage))
        {
            _logger.LogError("Empty message.");
            return;
        }

        try
        {
            receivedMessage = JsonSerializer.Deserialize<ExpectedMessage>(triggeredMessage, JsonSerializationOptions);
        }
        catch (JsonException ex)
        {
            _logger.LogError(ex, "Invalid message.");
            return;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected Error.");
            return;
        }

    } 

I am new to xUnit and Azure Service Bus. I could not find unit test examples using xUnit framework. Appreciate your help in giving me some sample code which shows how I can mock and write the test for the below function?

    [Function("ReceiveMessageFromTopic")]
    public void Run([ServiceBusTrigger("myTopic", "myTopicSubscription", Connection = "ServiceBusConnection")] string triggeredMessage)
    {
        ExpectedMessage receivedMessage = new();
        _logger.LogInformation(
quot;ServiceBus topic trigger function processed message: {triggeredMessage}");

        if (string.IsNullOrEmpty(triggeredMessage))
        {
            _logger.LogError("Empty message.");
            return;
        }

        try
        {
            receivedMessage = JsonSerializer.Deserialize<ExpectedMessage>(triggeredMessage, JsonSerializationOptions);
        }
        catch (JsonException ex)
        {
            _logger.LogError(ex, "Invalid message.");
            return;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Unexpected Error.");
            return;
        }

    } 

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2025-01-18 23:36:51

要使用 azure 函数通过服务总线触发器进行 xunit 测试,您可以在此 Github 链接

有关更多信息,请参阅此SO THREAD 正如 @Sean Feldman 和这个 博客|.NET: Azure 服务总线单元测试

To do the xunit test with service bus trigger using azure function you can find the sample code in this Github link

For more information please refer this SO THREAD as suggested by @Sean Feldman and this Blog|.NET: Unit Testing Azure Service Bus

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文