4

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);
Imran
  • 65
  • 2
  • 7
  • 2
    use dependency injection and pass CustomSection in the constructor. – Thiago Custodio Apr 10 '20 at 17:33
  • Thanks. I am following [link](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#working-with-options-and-settings) which works for settings like "Values": { "MyOptions:MyCustomSetting": "Foobar" } but won't work for "MyOptions": { "MyCustomSetting": "Foobar" }. Any ideas? – Imran Apr 11 '20 at 22:54
  • I ended up following [this](https://stackoverflow.com/a/56500317/965676) – Imran Apr 11 '20 at 23:27

3 Answers3

1

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);
0

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

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

  • 2
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Oct 07 '22 at 05:46
0

ExecutionContext belongs to NuGet Microsoft.Azure.WebJobs.ExecutionContext. And you can mock FunctionAppDirectory like so:

var mockContext = new Mock<Microsoft.Azure.WebJobs.ExecutionContext>();
mockContext 
    .Setup(x => x.FunctionAppDirectory)
    .Returns(Directory.GetCurrentDirectory()); // Add your directory value inside Returns

var theContextObject = mockContext.Object;

theContextObject is the object instance you can pass in. When FunctionAppDirectory is called, your current directory is going to be returned. You can tweak inside of Returns according to what you want FunctionAppDirectory to be returned.

Serhat
  • 216
  • 2
  • 17