How can I use or mock IWebJobsBuilder to do an integration test of my Azure Function v2?What is the difference between integration and unit tests?How can I get the application's path in a .NET console application?How can I generate random alphanumeric strings?What's the difference between unit tests and integration tests?How to register and use different implementation of same interface?How can I use NuGet packages in my Azure Functions?Azure Webjobs vs Azure Functions : How to chooseBootstrapping TestServer with TestStartup with InMemoryDatabase fails (.Net core)Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock?Azure Functions HTTP integration testing

Polynomial division: Is this trick obvious?

Would it be fair to use 1d30 (instead of rolling 2d20 and taking the higher die) for advantage rolls?

Would life always name the light from their sun "white"

When did Britain learn about American independence?

How was the blinking terminal cursor invented?

Why are lawsuits between the President and Congress not automatically sent to the Supreme Court

Can EU citizens work on Iceland?

Why do galaxies collide?

Find the area of the rectangle

Why is Drogon so much better in battle than Rhaegal and Viserion?

Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?

Why is the A380’s with-reversers stopping distance the same as its no-reversers stopping distance?

301 Redirects what does ([a-z]+)-(.*) and ([0-9]+)-(.*) mean

How can I safely determine the output voltage and current of a transformer?

Cannot remove door knob -- totally inaccessible!

How can we delete item permanently without storing in Recycle Bin?

I recently started my machine learning PhD and I have absolutely no idea what I'm doing

How could it be that 80% of townspeople were farmers during the Edo period in Japan?

What is this rubber on gear cables

Pedaling at different gear ratios on flat terrain: what's the point?

Square spiral in Mathematica

Why do academics prefer Mac/Linux?

Why does the U.S military use mercenaries?

Omit property variable when using object destructuring



How can I use or mock IWebJobsBuilder to do an integration test of my Azure Function v2?


What is the difference between integration and unit tests?How can I get the application's path in a .NET console application?How can I generate random alphanumeric strings?What's the difference between unit tests and integration tests?How to register and use different implementation of same interface?How can I use NuGet packages in my Azure Functions?Azure Webjobs vs Azure Functions : How to chooseBootstrapping TestServer with TestStartup with InMemoryDatabase fails (.Net core)Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock?Azure Functions HTTP integration testing






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.



public sealed class CreateAccountFunction

private readonly IAccountWorkflow m_accountWorkflow;

private readonly ILogger<CreateAccountFunction> m_logger;

private readonly IMapper m_mapper;

public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)

m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;


[FunctionName("CreateAccount")]
public async Task<IActionResult> Run(
[HttpTrigger(
AuthorizationLevel.Function,
"post",
Route = "v1/accounts/"
)]
HttpRequest httpRequest)

// Creates the account.




My Startup class contains the following:



public sealed class Startup : IWebJobsStartup

public void Configure(IWebJobsBuilder webJobsBuilder)

webJobsBuilder.Services.AddLogging(loggingBuilder =>

loggingBuilder.SetMinimumLevel(LogLevel.Debug);
);

var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());

webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();




Now I would like to do an integration tests of the Azure Function.



public class CreateAccountFunctionTests

private readonly CreateAccountFunction m_creationAccountFunction;


public CreateAccountFunctionTests()

// --> How can I reuse the Startup and IWebJobsBuilder <--
m_creationAccountFunction = new CreateAccountFunction(? ? ?);


[Fact]
public void TestSomething()

// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */

// Act.
var result = m_creationAccountFunction.Run(httpRequest);

// Assert.
// Asserts the Status Code.




Question



It looks a lot of the injection stuff is handled by IWebJobsBuilder.



How can I leverage this to do integration tests of my Azure Functions?



I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.










share|improve this question




























    1















    I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.



    public sealed class CreateAccountFunction

    private readonly IAccountWorkflow m_accountWorkflow;

    private readonly ILogger<CreateAccountFunction> m_logger;

    private readonly IMapper m_mapper;

    public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)

    m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
    m_mapper = mapper;
    m_accountWorkflow = accountWorkflow;


    [FunctionName("CreateAccount")]
    public async Task<IActionResult> Run(
    [HttpTrigger(
    AuthorizationLevel.Function,
    "post",
    Route = "v1/accounts/"
    )]
    HttpRequest httpRequest)

    // Creates the account.




    My Startup class contains the following:



    public sealed class Startup : IWebJobsStartup

    public void Configure(IWebJobsBuilder webJobsBuilder)

    webJobsBuilder.Services.AddLogging(loggingBuilder =>

    loggingBuilder.SetMinimumLevel(LogLevel.Debug);
    );

    var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
    webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());

    webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();




    Now I would like to do an integration tests of the Azure Function.



    public class CreateAccountFunctionTests

    private readonly CreateAccountFunction m_creationAccountFunction;


    public CreateAccountFunctionTests()

    // --> How can I reuse the Startup and IWebJobsBuilder <--
    m_creationAccountFunction = new CreateAccountFunction(? ? ?);


    [Fact]
    public void TestSomething()

    // Arrange.
    HttpRequest httpRequest = /* builds an instance of HttpRequest */

    // Act.
    var result = m_creationAccountFunction.Run(httpRequest);

    // Assert.
    // Asserts the Status Code.




    Question



    It looks a lot of the injection stuff is handled by IWebJobsBuilder.



    How can I leverage this to do integration tests of my Azure Functions?



    I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.










    share|improve this question
























      1












      1








      1


      1






      I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.



      public sealed class CreateAccountFunction

      private readonly IAccountWorkflow m_accountWorkflow;

      private readonly ILogger<CreateAccountFunction> m_logger;

      private readonly IMapper m_mapper;

      public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)

      m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
      m_mapper = mapper;
      m_accountWorkflow = accountWorkflow;


      [FunctionName("CreateAccount")]
      public async Task<IActionResult> Run(
      [HttpTrigger(
      AuthorizationLevel.Function,
      "post",
      Route = "v1/accounts/"
      )]
      HttpRequest httpRequest)

      // Creates the account.




      My Startup class contains the following:



      public sealed class Startup : IWebJobsStartup

      public void Configure(IWebJobsBuilder webJobsBuilder)

      webJobsBuilder.Services.AddLogging(loggingBuilder =>

      loggingBuilder.SetMinimumLevel(LogLevel.Debug);
      );

      var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
      webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());

      webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();




      Now I would like to do an integration tests of the Azure Function.



      public class CreateAccountFunctionTests

      private readonly CreateAccountFunction m_creationAccountFunction;


      public CreateAccountFunctionTests()

      // --> How can I reuse the Startup and IWebJobsBuilder <--
      m_creationAccountFunction = new CreateAccountFunction(? ? ?);


      [Fact]
      public void TestSomething()

      // Arrange.
      HttpRequest httpRequest = /* builds an instance of HttpRequest */

      // Act.
      var result = m_creationAccountFunction.Run(httpRequest);

      // Assert.
      // Asserts the Status Code.




      Question



      It looks a lot of the injection stuff is handled by IWebJobsBuilder.



      How can I leverage this to do integration tests of my Azure Functions?



      I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.










      share|improve this question














      I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.



      public sealed class CreateAccountFunction

      private readonly IAccountWorkflow m_accountWorkflow;

      private readonly ILogger<CreateAccountFunction> m_logger;

      private readonly IMapper m_mapper;

      public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)

      m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
      m_mapper = mapper;
      m_accountWorkflow = accountWorkflow;


      [FunctionName("CreateAccount")]
      public async Task<IActionResult> Run(
      [HttpTrigger(
      AuthorizationLevel.Function,
      "post",
      Route = "v1/accounts/"
      )]
      HttpRequest httpRequest)

      // Creates the account.




      My Startup class contains the following:



      public sealed class Startup : IWebJobsStartup

      public void Configure(IWebJobsBuilder webJobsBuilder)

      webJobsBuilder.Services.AddLogging(loggingBuilder =>

      loggingBuilder.SetMinimumLevel(LogLevel.Debug);
      );

      var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
      webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());

      webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();




      Now I would like to do an integration tests of the Azure Function.



      public class CreateAccountFunctionTests

      private readonly CreateAccountFunction m_creationAccountFunction;


      public CreateAccountFunctionTests()

      // --> How can I reuse the Startup and IWebJobsBuilder <--
      m_creationAccountFunction = new CreateAccountFunction(? ? ?);


      [Fact]
      public void TestSomething()

      // Arrange.
      HttpRequest httpRequest = /* builds an instance of HttpRequest */

      // Act.
      var result = m_creationAccountFunction.Run(httpRequest);

      // Assert.
      // Asserts the Status Code.




      Question



      It looks a lot of the injection stuff is handled by IWebJobsBuilder.



      How can I leverage this to do integration tests of my Azure Functions?



      I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.







      c# azure dependency-injection integration-testing azure-functions






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 16:16









      KzrystofKzrystof

      2,29231628




      2,29231628






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I looked into the Azure Function host code and found this section of code in the Program.cs file:



          var host = new HostBuilder()
          .SetAzureFunctionsEnvironment()
          .ConfigureLogging(b =>

          b.SetMinimumLevel(LogLevel.Information);
          b.AddConsole();
          )
          .AddScriptHost(options, webJobsBuilder =>

          webJobsBuilder.AddAzureStorageCoreServices();
          )
          .UseConsoleLifetime()
          .Build();


          The part that got me interested was the AddScriptHost() extension method, which makes the webJobsBuilder instance (an implementation of IWebJobsBuilder) available.



          Knowing that, I created the following method which creates a simple IHost instance and uses my existing Startup class which contains all the injected services:



          /// <summary>
          /// Builds an instance of the specified <typeparamref name="TFunctionType"/>
          /// with the services defined in the <paramref name="startup"/> instance.
          /// </summary>
          /// <typeparam name="TFunctionType"></typeparam>
          /// <param name="startup"></param>
          /// <returns></returns>
          /// <exception cref="ArgumentNullException">
          /// Thrown if:
          /// - The <paramref name="startup" /> instance is not specified.
          /// </exception>
          public static TFunctionType Instanciate<TFunctionType>(Startup startup)

          Argument.ThrowIfIsNull(startup, nameof(startup));

          // --> Builds an IHost with all the services registered in the Startup.
          IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();

          return Instanciate<TFunctionType>(host);



          The Instanciate<TFunctionType> method looks for a constructor of TFunctionType and retrieves all the services from the IHost instance:



          /// <summary>
          /// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
          /// </summary>
          /// <typeparam name="TFunctionType"></typeparam>
          /// <param name="host"></param>
          /// <returns></returns>
          private static TFunctionType Instanciate<TFunctionType>(IHost host)

          Type type = typeof(TFunctionType);

          // --> This part could be better...
          ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();

          ParameterInfo[] parametersInfo = contructorInfo.GetParameters();

          object[] parameters = LookupServiceInstances(host, parametersInfo);

          return (TFunctionType) Activator.CreateInstance(type, parameters);


          /// <summary>
          /// Gets all the parameters instances from the host's services.
          /// </summary>
          /// <param name="host"></param>
          /// <param name="parametersInfo"></param>
          /// <returns></returns>
          private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)

          return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
          .ToArray();



          I put these methods in an HostHelper class. Now, in my test, I can reuse the Startup class.



          Even better, I can subclass Startup so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:



          public class CreateAccountFunctionTests

          private readonly CreateAccountFunction m_creationAccountFunction;

          public CreateAccountFunctionTests()

          var startup = new Startup();

          m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);


          [Fact]
          public void TestSomething()

          // Arrange.
          HttpRequest httpRequest = /* builds an instance of HttpRequest */

          // Act.
          var result = m_creationAccountFunction.Run(httpRequest);

          // Assert.
          // Asserts the Status Code.







          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315785%2fhow-can-i-use-or-mock-iwebjobsbuilder-to-do-an-integration-test-of-my-azure-func%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I looked into the Azure Function host code and found this section of code in the Program.cs file:



            var host = new HostBuilder()
            .SetAzureFunctionsEnvironment()
            .ConfigureLogging(b =>

            b.SetMinimumLevel(LogLevel.Information);
            b.AddConsole();
            )
            .AddScriptHost(options, webJobsBuilder =>

            webJobsBuilder.AddAzureStorageCoreServices();
            )
            .UseConsoleLifetime()
            .Build();


            The part that got me interested was the AddScriptHost() extension method, which makes the webJobsBuilder instance (an implementation of IWebJobsBuilder) available.



            Knowing that, I created the following method which creates a simple IHost instance and uses my existing Startup class which contains all the injected services:



            /// <summary>
            /// Builds an instance of the specified <typeparamref name="TFunctionType"/>
            /// with the services defined in the <paramref name="startup"/> instance.
            /// </summary>
            /// <typeparam name="TFunctionType"></typeparam>
            /// <param name="startup"></param>
            /// <returns></returns>
            /// <exception cref="ArgumentNullException">
            /// Thrown if:
            /// - The <paramref name="startup" /> instance is not specified.
            /// </exception>
            public static TFunctionType Instanciate<TFunctionType>(Startup startup)

            Argument.ThrowIfIsNull(startup, nameof(startup));

            // --> Builds an IHost with all the services registered in the Startup.
            IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();

            return Instanciate<TFunctionType>(host);



            The Instanciate<TFunctionType> method looks for a constructor of TFunctionType and retrieves all the services from the IHost instance:



            /// <summary>
            /// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
            /// </summary>
            /// <typeparam name="TFunctionType"></typeparam>
            /// <param name="host"></param>
            /// <returns></returns>
            private static TFunctionType Instanciate<TFunctionType>(IHost host)

            Type type = typeof(TFunctionType);

            // --> This part could be better...
            ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();

            ParameterInfo[] parametersInfo = contructorInfo.GetParameters();

            object[] parameters = LookupServiceInstances(host, parametersInfo);

            return (TFunctionType) Activator.CreateInstance(type, parameters);


            /// <summary>
            /// Gets all the parameters instances from the host's services.
            /// </summary>
            /// <param name="host"></param>
            /// <param name="parametersInfo"></param>
            /// <returns></returns>
            private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)

            return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
            .ToArray();



            I put these methods in an HostHelper class. Now, in my test, I can reuse the Startup class.



            Even better, I can subclass Startup so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:



            public class CreateAccountFunctionTests

            private readonly CreateAccountFunction m_creationAccountFunction;

            public CreateAccountFunctionTests()

            var startup = new Startup();

            m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);


            [Fact]
            public void TestSomething()

            // Arrange.
            HttpRequest httpRequest = /* builds an instance of HttpRequest */

            // Act.
            var result = m_creationAccountFunction.Run(httpRequest);

            // Assert.
            // Asserts the Status Code.







            share|improve this answer





























              0














              I looked into the Azure Function host code and found this section of code in the Program.cs file:



              var host = new HostBuilder()
              .SetAzureFunctionsEnvironment()
              .ConfigureLogging(b =>

              b.SetMinimumLevel(LogLevel.Information);
              b.AddConsole();
              )
              .AddScriptHost(options, webJobsBuilder =>

              webJobsBuilder.AddAzureStorageCoreServices();
              )
              .UseConsoleLifetime()
              .Build();


              The part that got me interested was the AddScriptHost() extension method, which makes the webJobsBuilder instance (an implementation of IWebJobsBuilder) available.



              Knowing that, I created the following method which creates a simple IHost instance and uses my existing Startup class which contains all the injected services:



              /// <summary>
              /// Builds an instance of the specified <typeparamref name="TFunctionType"/>
              /// with the services defined in the <paramref name="startup"/> instance.
              /// </summary>
              /// <typeparam name="TFunctionType"></typeparam>
              /// <param name="startup"></param>
              /// <returns></returns>
              /// <exception cref="ArgumentNullException">
              /// Thrown if:
              /// - The <paramref name="startup" /> instance is not specified.
              /// </exception>
              public static TFunctionType Instanciate<TFunctionType>(Startup startup)

              Argument.ThrowIfIsNull(startup, nameof(startup));

              // --> Builds an IHost with all the services registered in the Startup.
              IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();

              return Instanciate<TFunctionType>(host);



              The Instanciate<TFunctionType> method looks for a constructor of TFunctionType and retrieves all the services from the IHost instance:



              /// <summary>
              /// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
              /// </summary>
              /// <typeparam name="TFunctionType"></typeparam>
              /// <param name="host"></param>
              /// <returns></returns>
              private static TFunctionType Instanciate<TFunctionType>(IHost host)

              Type type = typeof(TFunctionType);

              // --> This part could be better...
              ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();

              ParameterInfo[] parametersInfo = contructorInfo.GetParameters();

              object[] parameters = LookupServiceInstances(host, parametersInfo);

              return (TFunctionType) Activator.CreateInstance(type, parameters);


              /// <summary>
              /// Gets all the parameters instances from the host's services.
              /// </summary>
              /// <param name="host"></param>
              /// <param name="parametersInfo"></param>
              /// <returns></returns>
              private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)

              return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
              .ToArray();



              I put these methods in an HostHelper class. Now, in my test, I can reuse the Startup class.



              Even better, I can subclass Startup so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:



              public class CreateAccountFunctionTests

              private readonly CreateAccountFunction m_creationAccountFunction;

              public CreateAccountFunctionTests()

              var startup = new Startup();

              m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);


              [Fact]
              public void TestSomething()

              // Arrange.
              HttpRequest httpRequest = /* builds an instance of HttpRequest */

              // Act.
              var result = m_creationAccountFunction.Run(httpRequest);

              // Assert.
              // Asserts the Status Code.







              share|improve this answer



























                0












                0








                0







                I looked into the Azure Function host code and found this section of code in the Program.cs file:



                var host = new HostBuilder()
                .SetAzureFunctionsEnvironment()
                .ConfigureLogging(b =>

                b.SetMinimumLevel(LogLevel.Information);
                b.AddConsole();
                )
                .AddScriptHost(options, webJobsBuilder =>

                webJobsBuilder.AddAzureStorageCoreServices();
                )
                .UseConsoleLifetime()
                .Build();


                The part that got me interested was the AddScriptHost() extension method, which makes the webJobsBuilder instance (an implementation of IWebJobsBuilder) available.



                Knowing that, I created the following method which creates a simple IHost instance and uses my existing Startup class which contains all the injected services:



                /// <summary>
                /// Builds an instance of the specified <typeparamref name="TFunctionType"/>
                /// with the services defined in the <paramref name="startup"/> instance.
                /// </summary>
                /// <typeparam name="TFunctionType"></typeparam>
                /// <param name="startup"></param>
                /// <returns></returns>
                /// <exception cref="ArgumentNullException">
                /// Thrown if:
                /// - The <paramref name="startup" /> instance is not specified.
                /// </exception>
                public static TFunctionType Instanciate<TFunctionType>(Startup startup)

                Argument.ThrowIfIsNull(startup, nameof(startup));

                // --> Builds an IHost with all the services registered in the Startup.
                IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();

                return Instanciate<TFunctionType>(host);



                The Instanciate<TFunctionType> method looks for a constructor of TFunctionType and retrieves all the services from the IHost instance:



                /// <summary>
                /// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
                /// </summary>
                /// <typeparam name="TFunctionType"></typeparam>
                /// <param name="host"></param>
                /// <returns></returns>
                private static TFunctionType Instanciate<TFunctionType>(IHost host)

                Type type = typeof(TFunctionType);

                // --> This part could be better...
                ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();

                ParameterInfo[] parametersInfo = contructorInfo.GetParameters();

                object[] parameters = LookupServiceInstances(host, parametersInfo);

                return (TFunctionType) Activator.CreateInstance(type, parameters);


                /// <summary>
                /// Gets all the parameters instances from the host's services.
                /// </summary>
                /// <param name="host"></param>
                /// <param name="parametersInfo"></param>
                /// <returns></returns>
                private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)

                return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
                .ToArray();



                I put these methods in an HostHelper class. Now, in my test, I can reuse the Startup class.



                Even better, I can subclass Startup so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:



                public class CreateAccountFunctionTests

                private readonly CreateAccountFunction m_creationAccountFunction;

                public CreateAccountFunctionTests()

                var startup = new Startup();

                m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);


                [Fact]
                public void TestSomething()

                // Arrange.
                HttpRequest httpRequest = /* builds an instance of HttpRequest */

                // Act.
                var result = m_creationAccountFunction.Run(httpRequest);

                // Assert.
                // Asserts the Status Code.







                share|improve this answer















                I looked into the Azure Function host code and found this section of code in the Program.cs file:



                var host = new HostBuilder()
                .SetAzureFunctionsEnvironment()
                .ConfigureLogging(b =>

                b.SetMinimumLevel(LogLevel.Information);
                b.AddConsole();
                )
                .AddScriptHost(options, webJobsBuilder =>

                webJobsBuilder.AddAzureStorageCoreServices();
                )
                .UseConsoleLifetime()
                .Build();


                The part that got me interested was the AddScriptHost() extension method, which makes the webJobsBuilder instance (an implementation of IWebJobsBuilder) available.



                Knowing that, I created the following method which creates a simple IHost instance and uses my existing Startup class which contains all the injected services:



                /// <summary>
                /// Builds an instance of the specified <typeparamref name="TFunctionType"/>
                /// with the services defined in the <paramref name="startup"/> instance.
                /// </summary>
                /// <typeparam name="TFunctionType"></typeparam>
                /// <param name="startup"></param>
                /// <returns></returns>
                /// <exception cref="ArgumentNullException">
                /// Thrown if:
                /// - The <paramref name="startup" /> instance is not specified.
                /// </exception>
                public static TFunctionType Instanciate<TFunctionType>(Startup startup)

                Argument.ThrowIfIsNull(startup, nameof(startup));

                // --> Builds an IHost with all the services registered in the Startup.
                IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();

                return Instanciate<TFunctionType>(host);



                The Instanciate<TFunctionType> method looks for a constructor of TFunctionType and retrieves all the services from the IHost instance:



                /// <summary>
                /// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
                /// </summary>
                /// <typeparam name="TFunctionType"></typeparam>
                /// <param name="host"></param>
                /// <returns></returns>
                private static TFunctionType Instanciate<TFunctionType>(IHost host)

                Type type = typeof(TFunctionType);

                // --> This part could be better...
                ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();

                ParameterInfo[] parametersInfo = contructorInfo.GetParameters();

                object[] parameters = LookupServiceInstances(host, parametersInfo);

                return (TFunctionType) Activator.CreateInstance(type, parameters);


                /// <summary>
                /// Gets all the parameters instances from the host's services.
                /// </summary>
                /// <param name="host"></param>
                /// <param name="parametersInfo"></param>
                /// <returns></returns>
                private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)

                return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
                .ToArray();



                I put these methods in an HostHelper class. Now, in my test, I can reuse the Startup class.



                Even better, I can subclass Startup so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:



                public class CreateAccountFunctionTests

                private readonly CreateAccountFunction m_creationAccountFunction;

                public CreateAccountFunctionTests()

                var startup = new Startup();

                m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);


                [Fact]
                public void TestSomething()

                // Arrange.
                HttpRequest httpRequest = /* builds an instance of HttpRequest */

                // Act.
                var result = m_creationAccountFunction.Run(httpRequest);

                // Assert.
                // Asserts the Status Code.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 24 at 16:22

























                answered Mar 24 at 14:51









                KzrystofKzrystof

                2,29231628




                2,29231628





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315785%2fhow-can-i-use-or-mock-iwebjobsbuilder-to-do-an-integration-test-of-my-azure-func%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현