Create XUnit test for configureappconfiguration

Brett McDonald 1 Reputation point
2020-12-22T00:26:04.62+00:00

I would like to create a test for configureappconfiguration using XUnit. I used .ConfigureWebJobs(startup.Configure) whic just calls the Confure method in my Startup.cs. I want to call configureappconfiguration so it will test that also. But having a hard time finding how to do that.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,300 questions
Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
209 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Krish G 2,326 Reputation points
    2020-12-23T08:26:27.023+00:00

    .ConfigureWebJobs has an overload which accepts delegate of configureAppConfiguration

    public static IHostBuilder ConfigureWebJobs(this IHostBuilder builder, Action<HostBuilderContext, IWebJobsBuilder> configure, Action<JobHostOptions> configureOptions, Action<HostBuilderContext, IWebJobsConfigurationBuilder> configureAppConfiguration)
    

    Using that as below in test, I could run startup test successfully which calls ConfigureAppConfiguration. Below is just a simple example. You can configure as per your need. Setting up the proper parameters is bit tricky to match Function interfaces.

    My startup.cs:

    [assembly: Microsoft.Azure.Functions.Extensions.DependencyInjection.FunctionsStartup(typeof(FunctionApp1.Startup))]
    namespace FunctionApp1
    {
        using Microsoft.Azure.Functions.Extensions.DependencyInjection;
        using Microsoft.Extensions.DependencyInjection;
    
        public class Startup : FunctionsStartup
        {
            public override void Configure(IFunctionsHostBuilder builder)
            {
                builder.Services.AddSingleton(typeof(IMyInterface<,>), typeof(MyClass<>));
            }
    
            public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
            {
                base.ConfigureAppConfiguration(builder);
            }
        }
    }
    

    My startup test:

    namespace XUnitTestProject1
    {
        using FunctionApp1;
    
        using Microsoft.Azure.Functions.Extensions.DependencyInjection;
        using Microsoft.Extensions.Hosting;
    
        using Moq;
    
        using Xunit;
    
        public class UnitTest1
        {
            [Fact]
            public void StartupTest()
            {
                var functionConfigurationBuilderMock = new Mock<IFunctionsConfigurationBuilder>();
                var startup = new Startup();
                var host = new HostBuilder()
                    .ConfigureWebJobs((c, b) => startup.Configure(b), 
                                       null,
                                       (c, b) => {
                                           functionConfigurationBuilderMock.SetupGet(m => m.ConfigurationBuilder).Returns(b.ConfigurationBuilder);
                                           startup.ConfigureAppConfiguration(functionConfigurationBuilderMock.Object);
                                       })
                    .Build();
    
                 // Assert blah blah
            }
        }
    }
    
    1 person found this answer helpful.

  2. singhh-msft 2,431 Reputation points
    2020-12-22T15:08:05.27+00:00

    Hello @Brett McDonald , thanks for reaching out to us, your question is appreciated.

    You can use Microsoft.AspNetCore.Mvc.Testing package to achieve this scenario. I have done a quick setup to show you how to do this.

    As I understand, you want to call GenericWebHostBuilder.ConfigureAppConfiguration method in your Unit Tests using XUnit. To do this please follow below steps:

    • Run the below command to add Microsoft.AspNetCore.Mvc.Testing package in your Test project:
         dotnet add  package Microsoft.AspNetCore.Mvc.Testing  
      
      The packages includes a WebApplicationFactory<TEntryPoint> class which is used to bootstrap the API in memory. This is convenient, as we don't need to have the API running before we run these tests.
    • Inject WebApplicationFactory<Api.Startup> in your constructor like below: 50486-48.png
    • Run the test. This will call CreateHostBuilder in your Program.cs, and thus, ConfigureAppConfiguration method will be called. Please see the call stack of this below: 50511-47.png

    *If you want to create custom WebApplicationfactory, you can check out this article as it has detailed and step-by-step process to do that. *

    ----------

    Please do not forget to "Accept the answer" if this response answers your query. This will help others in the community as well.