使用Xunit的ServiceBustrigger单元测试
我的azure函数如下。在以下代码中,我将使用存储库中的数据库添加产品& EF核心。有什么办法可以使用Xunit
对其进行测试?目前,我正在使用Azure Service Bus Explorer
对此进行测试。
[FunctionName("SaveProductData")]
public void Run([ServiceBusTrigger("mytopicname", Connection = "ServiceBusConnectionString")]
ProductItemUpdate message, ILogger log)
{
var product = new Domain.Entities.Product()
{
... prop init goes here.....
};
log.LogInformation($"Processed Product - Sain: {message.ProductId}");
_productRepository.Add(product);
}
I have an Azure Function as below. In the following code, I'm adding a product to the database using Repository & EF Core. Is there any way we can test it using Xunit
? Currently, I'm testing this using Azure Service Bus Explorer
.
[FunctionName("SaveProductData")]
public void Run([ServiceBusTrigger("mytopicname", Connection = "ServiceBusConnectionString")]
ProductItemUpdate message, ILogger log)
{
var product = new Domain.Entities.Product()
{
... prop init goes here.....
};
log.LogInformation(quot;Processed Product - Sain: {message.ProductId}");
_productRepository.Add(product);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要详细说明ScottdavidWalker的评论,其中有一个完整的示例:
您需要创建您正在测试的类的实例并模拟依赖关系。
Azure函数本质上是类(
.run()
)的方法(您可以在实例上调用)。在您的单元测试中,您创建数据以触发方法,然后在模型上对代码实现时期望发生的事情做出断言。
To elaborate on scottdavidwalker's comment with a full example:
You need to create an instance of the class you are testing and mock the dependencies.
The Azure function is essentially a method (
.Run()
) inside the class which you can call on the instance.In your unit test, you create the data to trigger the method and then make assertions on your mocks for what you expect to happen when the code runs for real.