azure - 在 Azure 队列触发功能单元测试中模拟 ExecutionContext
问题描述
测试队列触发功能时如何模拟 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);
推荐阅读
- javascript - 在 React Native 中对 SVG 文件使用 require
- javascript - window.open with _blank 在 Firefox 中打开两个标签页
- javascript - 如果参数是另一个函数的返回值,该函数将不起作用
- linux - 使用 grep 精确匹配单词
- r - 将数据框列名更改为小写时使用 set_names 与 mutate(colnames)
- android - DataSnapShot 对象的值返回 0 而不是实际值
- azure - Azure 容器注册表 - 删除除 2 之外的所有映像
- python-3.x - 使用元组对实例的“装饰”列表进行排序 - Zelle Ex。11.9
- ubuntu - Unable to Run Amethyst Starter Template: 'VkError(ERROR_INCOMPATIBLE_DRIVER)'
- macos - SceneKit 在 macOS Catalyst 应用程序中是否可以继续工作?