Testing Configure(IFunctionsHostBuilder builder) which uses builder.GetContext().Configuration.GetValue to get appsettings

Paul Bush 21 Reputation points
2021-03-25T10:45:50.673+00:00

I have an function which on StartUp calls void Configure(IFunctionsHostBuilder builder) and one of the test required is to ensure DI is working properly. The problem I have encountered is because I'm using builder.Services.AddHttpClient and then configuring it using information from the appsettings this is failing as there is no context and this call fails builder.GetContext().Configuration.GetValue<string>("CustomSettings:MyTest"); Does anyone know how I could test this via XUnit or is it not possible?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,299 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,591 Reputation points Microsoft Employee
    2021-03-26T06:58:54.11+00:00

    @Paul Bush One of the main advantages of using DI is to allow testing without hard dependencies. You have full control to mock dependencies and test your function code directly.

    But it is still possible to test the configure code by setting up the webjobs container as shown in this blog. The idea here being you setup the underlying webjobs context while is the core of how Functions work.

    Another approach would of course just involve starting up the local function host and test your functions by directly triggering the function via HTTP or the Admin API for Non-HTTP Functions.

    0 comments No comments

0 additional answers

Sort by: Most helpful