azure - Azure队列中的Mocking ExecutionContext触发功能单元测试

标签 azure unit-testing azure-functions

测试队列触发函数时如何模拟 ExecutionContext?下面是示例函数,我在其中使用 ExecutionContext 获取一些设置值,我将在函数中进一步使用这些值,我还需要为这些设置提供测试值。

[FunctionName("ProcessQueueFunction")]
public static void Run([QueueTrigger("process-queue", Connection = "AzureWebJobsStorage")]string queueItem, ILogger log, ExecutionContext context)
{
   var config = new ConfigurationBuilder()
       .SetBasePath(context.FunctionAppDirectory)
       .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
       .AddEnvironmentVariables()
       .Build();

       var customConfiguraion = config.GetSection("CustomSection");

       //do stuff...
}

我可以做一些链接,但这会给我错误,FunctionAppDirectory 无效或为空。我需要将 FunctionAppDirectory 设置为什么?或者这是一个不正确的方法?

var mockContext = new Mock<ExecutionContext>();
mockContext.Object.FunctionAppDirectory = string.Empty; //or any other value

ProcessQueueFunction.Run("TestQueueItem", log: logger, context: mockContext.Object);

最佳答案

您可以直接传递执行上下文,无需仅仅为了使用属性而模拟类。

ExecutionContext executionContext = new ExecutionContext();
executionContext.FunctionAppDirectory = "PATH";

ProcessQueueFunction.Run("TestQueueItem", log: logger, context: executionContext);

关于azure - Azure队列中的Mocking ExecutionContext触发功能单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61145526/

相关文章:

c# - 验证 IEnumerable<dynamic> 内容的单元测试

c# - NUnit测试调试

azure - 对于每个事件,azure 是否在消费计划中创建 Azure 函数的新实例

node.js - 如何在Azure函数HttpTriggers( Node )中使用cookie

azure - 在 Azure Dev Ops 代理上安装 Az 2.8.0 Powershell 模块

c# - Telerik Reporting 在本地生成 pdf,但不在 Azure 上生成

azure - 如何执行一次 Azure 函数?

azure - 如何自动使 Azure 服务总线订阅上的消息过期?

c# - 我应该断言验证方法已执行吗?

azure - 检测到程序集更改。重启主机