简体   繁体   English

Azure 函数中的单元测试 RabbitMQ 使用 xUnit 测试

[英]Unit Testing in Azure Functions RabbitMQ using xUnit Test

I did some googling based on how to do xUnit Test for Azure Functions, but the sample I saw was using HTTPRequest .我根据如何对 Azure 函数进行 xUnit 测试进行了一些谷歌搜索,但我看到的示例使用的是HTTPRequest

Is there a way of doing xUnit Test for RabbitMQ?有没有办法对 RabbitMQ 进行 xUnit 测试? This is my function:这是我的 function:

    [FunctionName("MyFirstAzureFunction")]
    public static async void RabbitMQTrigger_MyFirstAzureFunction(
        [RabbitMQTrigger("MyFirstAzureFunction.Queue", ConnectionStringSetting = "ConnString")]string inputEvent, 
        [RabbitMQ(QueueName = "MyFirstAzureFunction.Queue", ConnectionStringSetting = "ConnString")]IAsyncCollector<string> outputEvent,
        ILogger log)
    {
        //SOME FUNCTION HERE
        return
        }
        catch (Exception ex)
        {
            await outputEvent.AddAsync(inputEvent);
        }
    }

As mentioned in a comment, you can mock the IAsyncCollector using Moq to verify whether the .AddAsync() method was called in your catch block with the expected input:如评论中所述,您可以使用Moq模拟IAsyncCollector以验证是否在您的catch块中使用预期输入调用了.AddAsync()方法:

[TestClass]
public class MyFirstAzureFunctionTests
{
    [TestMethod]
    public async Task GivenRabbitMQTrigger_WhenException_ThenInputEventAddedToOutputEventCollector()
    {
        // arrange
        var mockOutputQueue = new Mock<IAsyncCollector<string>>();
        var mockLogger = new Mock<ILogger>();

        var inputEvent = "foo";

        // act
        await MyFirstAzureFunction.Run(inputEvent, mockOutputQueue.Object, mockLogger.Object);

        // assert
        mockOutputQueue
            .Verify(x => x.AddAsync(It.Is<string>(s => s == inputEvent), It.IsAny<CancellationToken>()), Times.Once);
    }
}

My example uses MS Test but hopefully you get the idea.我的示例使用 MS Test,但希望您能理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM