测试队列触发函数时如何模拟 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/