Mocking ExecutionContext in Azure queue trigger function unit tests

Viewed 2174

How can I mock ExecutionContext when testing a queue trigger function? Below is the sample function where I am using ExecutionContext to getting some setting values which I use further down the function, I will need to provide testing values for these settings as well.

[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...
}

I can do something link this but that gives me error on the FunctionAppDirectory being invalid or empty. What do I need to set FunctionAppDirectory to? or is this an incorrect way to do this?

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

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

You can directly pass Execution Context, no need to mock a class just to use a property.

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

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