使用Xunit的ServiceBustrigger单元测试

发布于 2025-01-24 14:48:46 字数 553 浏览 2 评论 0原文

我的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 技术交流群。

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

发布评论

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

评论(1

苯莒 2025-01-31 14:48:46

要详细说明ScottdavidWalker的评论,其中有一个完整的示例:

public class SaveProductDataTests
{
    private readonly Mock<IProductRepository> _mockProductRepository = new Mock<IProductRepository>();
    private readonly Mock<ILogger> _mockLogger = new Mock<ILogger>();

    private readonly SaveProductData _saveProductData;

    public SaveProductDataTests()
    {
        _saveProductData = new SaveProductData(_mockProductRepository.Object);
    }

    [Fact]
    public void Given_When_Then()
    {
        // arrange
        var productItemUpdate = new ProductItemUpdate();

        // act
        _saveProductData.Run(productItemUpdate, _mockLogger.Object);

        // assert
        _mockProductRepository.Verify(x => x.Add(It.Is<Product>(p => p.SomeProperty == "xyz")));
    }
}

您需要创建您正在测试的类的实例并模拟依赖关系。

Azure函数本质上是类(.run())的方法(您可以在实例上调用)。

在您的单元测试中,您创建数据以触发方法,然后在模型上对代码实现时期望发生的事情做出断言。

To elaborate on scottdavidwalker's comment with a full example:

public class SaveProductDataTests
{
    private readonly Mock<IProductRepository> _mockProductRepository = new Mock<IProductRepository>();
    private readonly Mock<ILogger> _mockLogger = new Mock<ILogger>();

    private readonly SaveProductData _saveProductData;

    public SaveProductDataTests()
    {
        _saveProductData = new SaveProductData(_mockProductRepository.Object);
    }

    [Fact]
    public void Given_When_Then()
    {
        // arrange
        var productItemUpdate = new ProductItemUpdate();

        // act
        _saveProductData.Run(productItemUpdate, _mockLogger.Object);

        // assert
        _mockProductRepository.Verify(x => x.Add(It.Is<Product>(p => p.SomeProperty == "xyz")));
    }
}

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.

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