Azure 服务总线主题触发器的 xUnit 测试
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用 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 thisBlog|.NET: Unit Testing Azure Service Bus