question

BushPaul-2667 avatar image
0 Votes"
BushPaul-2667 asked PramodValavala-MSFT answered

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PramodValavala-MSFT avatar image
0 Votes"
PramodValavala-MSFT answered

@BushPaul-2667 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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.