Typed Configuration in Functional Test whit XUnit c# The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceWhy the TestServer (AspNetCore) gives 404 error on static files?VSTest.Console.exe ignores test with xunit BeforeAfterTestAttributeAdding additional services to IServiceCollection in Startup class for Integration test in asp.net core 2.0Unresolved constructor arguments error when trying to use dependency injection with XUnitAutomapper instance configuration has not been initialized in xUnit testHow to use Microsoft.Extensions.Configuration.IConiguration in my XUnit unit testingFailed to load http://localhost:5000/.well-known/openid-configuration: No 'Access-Control-Allow-Origin' header is present on the requested resourceAzure Devops Cant Run xUnit testsxUnit: how to Assert other event of types then EventHandler<EventArgs>TestStartup contained in a TestProject that inherits from API.Startup (contained in separate project) causes 404 error

Keeping a retro style to sci-fi spaceships?

Windows 10: How to Lock (not sleep) laptop on lid close?

Can a 1st-level character have an ability score above 18?

Working through the single responsibility principle (SRP) in Python when calls are expensive

How to pronounce 1ターン?

How are presidential pardons supposed to be used?

What's the point in a preamp?

He got a vote 80% that of Emmanuel Macron’s

Problems with Ubuntu mount /tmp

Does Parliament need to approve the new Brexit delay to 31 October 2019?

Did the new image of black hole confirm the general theory of relativity?

How to delete random line from file using Unix command?

How can I define good in a religion that claims no moral authority?

Simulating Exploding Dice

system() function string length limit

Can a novice safely splice in wire to lengthen 5V charging cable?

Simulation of a banking system with an Account class in C++

How to stretch delimiters to envolve matrices inside of a kbordermatrix?

Why can't devices on different VLANs, but on the same subnet, communicate?

Make it rain characters

How do I add random spotting to the same face in cycles?

What information about me do stores get via my credit card?

Why's the Graph of this Function so Wonky?

Single author papers against my advisor's will?



Typed Configuration in Functional Test whit XUnit c#



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceWhy the TestServer (AspNetCore) gives 404 error on static files?VSTest.Console.exe ignores test with xunit BeforeAfterTestAttributeAdding additional services to IServiceCollection in Startup class for Integration test in asp.net core 2.0Unresolved constructor arguments error when trying to use dependency injection with XUnitAutomapper instance configuration has not been initialized in xUnit testHow to use Microsoft.Extensions.Configuration.IConiguration in my XUnit unit testingFailed to load http://localhost:5000/.well-known/openid-configuration: No 'Access-Control-Allow-Origin' header is present on the requested resourceAzure Devops Cant Run xUnit testsxUnit: how to Assert other event of types then EventHandler<EventArgs>TestStartup contained in a TestProject that inherits from API.Startup (contained in separate project) causes 404 error



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








2















I am writing functional tests for my API with XUnit.
I have configured my API using types. Something like this:



public void ConfigureServices(IServiceCollection services)

services.ConfigureServices(services)
.Configure<SqlServerData>(Configuration.GetSection(nameof(SqlServerData)))
.Configure<SolrData>(Configuration.GetSection(nameof(SolrData)));



The problem comes when I try to configure my TestServer of the Microsoft.AspNetCore.TestHost class in the same way:



 Server = new TestServer(
new WebHostBuilder()
.ConfigureServices(services =>

//SOME STUFF
)
.Configure(Configuration.GetSection(nameof(SqlServerData)))
.UseStartup<TestStartup>()
);


I get the following error




cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>











share|improve this question
























  • TestStartup should inherit from your SUT's Startup class. As such, it's completely unnecessary to add this configuration again, in the first place.

    – Chris Pratt
    Mar 8 at 14:30

















2















I am writing functional tests for my API with XUnit.
I have configured my API using types. Something like this:



public void ConfigureServices(IServiceCollection services)

services.ConfigureServices(services)
.Configure<SqlServerData>(Configuration.GetSection(nameof(SqlServerData)))
.Configure<SolrData>(Configuration.GetSection(nameof(SolrData)));



The problem comes when I try to configure my TestServer of the Microsoft.AspNetCore.TestHost class in the same way:



 Server = new TestServer(
new WebHostBuilder()
.ConfigureServices(services =>

//SOME STUFF
)
.Configure(Configuration.GetSection(nameof(SqlServerData)))
.UseStartup<TestStartup>()
);


I get the following error




cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>











share|improve this question
























  • TestStartup should inherit from your SUT's Startup class. As such, it's completely unnecessary to add this configuration again, in the first place.

    – Chris Pratt
    Mar 8 at 14:30













2












2








2








I am writing functional tests for my API with XUnit.
I have configured my API using types. Something like this:



public void ConfigureServices(IServiceCollection services)

services.ConfigureServices(services)
.Configure<SqlServerData>(Configuration.GetSection(nameof(SqlServerData)))
.Configure<SolrData>(Configuration.GetSection(nameof(SolrData)));



The problem comes when I try to configure my TestServer of the Microsoft.AspNetCore.TestHost class in the same way:



 Server = new TestServer(
new WebHostBuilder()
.ConfigureServices(services =>

//SOME STUFF
)
.Configure(Configuration.GetSection(nameof(SqlServerData)))
.UseStartup<TestStartup>()
);


I get the following error




cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>











share|improve this question
















I am writing functional tests for my API with XUnit.
I have configured my API using types. Something like this:



public void ConfigureServices(IServiceCollection services)

services.ConfigureServices(services)
.Configure<SqlServerData>(Configuration.GetSection(nameof(SqlServerData)))
.Configure<SolrData>(Configuration.GetSection(nameof(SolrData)));



The problem comes when I try to configure my TestServer of the Microsoft.AspNetCore.TestHost class in the same way:



 Server = new TestServer(
new WebHostBuilder()
.ConfigureServices(services =>

//SOME STUFF
)
.Configure(Configuration.GetSection(nameof(SqlServerData)))
.UseStartup<TestStartup>()
);


I get the following error




cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<Microsoft.AspNetCore.Builder.IApplicationBuilder>








c# asp.net-core xunit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:51









Camilo Terevinto

19.8k64069




19.8k64069










asked Mar 8 at 13:43









Vicente MorenoVicente Moreno

132




132












  • TestStartup should inherit from your SUT's Startup class. As such, it's completely unnecessary to add this configuration again, in the first place.

    – Chris Pratt
    Mar 8 at 14:30

















  • TestStartup should inherit from your SUT's Startup class. As such, it's completely unnecessary to add this configuration again, in the first place.

    – Chris Pratt
    Mar 8 at 14:30
















TestStartup should inherit from your SUT's Startup class. As such, it's completely unnecessary to add this configuration again, in the first place.

– Chris Pratt
Mar 8 at 14:30





TestStartup should inherit from your SUT's Startup class. As such, it's completely unnecessary to add this configuration again, in the first place.

– Chris Pratt
Mar 8 at 14:30












2 Answers
2






active

oldest

votes


















1














Configure method is defined in WebHostBuilderExtensions class in the Microsoft.AspNetCore.Hosting assembly and its signature is as follows:



public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder,
Action<IApplicationBuilder> configureApp)

...



This means you must pass an action containing an IApplicationBuilder implementation instance. Something similar you can find in the Startup class in the API you're testing.



public void Configure(IApplicationBuilder app)




Other dependencies can also be injected in the Configure method, provided they're registered with the DI container you're using. Configure() is called by the runtime so you normally don't need to explicitly invoke it.



If what you need is to get an instance of your configuration in the Configure method, you can get it (or any other registered dependency) from the IServiceProvider instance that is exposed by the IApplicationBuilder object of the configuring action you pass to the method:



Server = new TestServer(new WebHostBuilder()
.Configure(appBuilder =>

//get the required service from the appBuilder's ApplicationServices property
var config = appBuilder.ApplicationServices.GetService<IConfiguration>();

var section = config.GetSection("YourSection");
//Use your section here.
)
.UseStartup<TestStartup>()


However, please be careful because at that point it might be already too late to setup a service. Configure() is mostly used to setup the request pipeline. Even though you might have a legit use case to use the configuration in it, you should verify you can't do it in the ConfigureServices() method instead.



Hope this helps!






share|improve this answer






























    0














    Very thanks for your response:



    Other thing is I needed to add a reference to the Nuget:



    Microsoft.Extensions.Options.ConfigurationExtensions


    Now I can do something like:



    Server = new TestServer(
    new WebHostBuilder()
    .ConfigureServices(services =>


    services.Configure<SqlServerData>(Configuration.GetSection("SqlServerData"));
    services.Configure<SolrData>(Configuration.GetSection("SolrData"));
    )
    .UseStartup<TestStartup>()
    );


    Thanks






    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%2f55064480%2ftyped-configuration-in-functional-test-whit-xunit-c-sharp%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Configure method is defined in WebHostBuilderExtensions class in the Microsoft.AspNetCore.Hosting assembly and its signature is as follows:



      public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder,
      Action<IApplicationBuilder> configureApp)

      ...



      This means you must pass an action containing an IApplicationBuilder implementation instance. Something similar you can find in the Startup class in the API you're testing.



      public void Configure(IApplicationBuilder app)




      Other dependencies can also be injected in the Configure method, provided they're registered with the DI container you're using. Configure() is called by the runtime so you normally don't need to explicitly invoke it.



      If what you need is to get an instance of your configuration in the Configure method, you can get it (or any other registered dependency) from the IServiceProvider instance that is exposed by the IApplicationBuilder object of the configuring action you pass to the method:



      Server = new TestServer(new WebHostBuilder()
      .Configure(appBuilder =>

      //get the required service from the appBuilder's ApplicationServices property
      var config = appBuilder.ApplicationServices.GetService<IConfiguration>();

      var section = config.GetSection("YourSection");
      //Use your section here.
      )
      .UseStartup<TestStartup>()


      However, please be careful because at that point it might be already too late to setup a service. Configure() is mostly used to setup the request pipeline. Even though you might have a legit use case to use the configuration in it, you should verify you can't do it in the ConfigureServices() method instead.



      Hope this helps!






      share|improve this answer



























        1














        Configure method is defined in WebHostBuilderExtensions class in the Microsoft.AspNetCore.Hosting assembly and its signature is as follows:



        public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder,
        Action<IApplicationBuilder> configureApp)

        ...



        This means you must pass an action containing an IApplicationBuilder implementation instance. Something similar you can find in the Startup class in the API you're testing.



        public void Configure(IApplicationBuilder app)




        Other dependencies can also be injected in the Configure method, provided they're registered with the DI container you're using. Configure() is called by the runtime so you normally don't need to explicitly invoke it.



        If what you need is to get an instance of your configuration in the Configure method, you can get it (or any other registered dependency) from the IServiceProvider instance that is exposed by the IApplicationBuilder object of the configuring action you pass to the method:



        Server = new TestServer(new WebHostBuilder()
        .Configure(appBuilder =>

        //get the required service from the appBuilder's ApplicationServices property
        var config = appBuilder.ApplicationServices.GetService<IConfiguration>();

        var section = config.GetSection("YourSection");
        //Use your section here.
        )
        .UseStartup<TestStartup>()


        However, please be careful because at that point it might be already too late to setup a service. Configure() is mostly used to setup the request pipeline. Even though you might have a legit use case to use the configuration in it, you should verify you can't do it in the ConfigureServices() method instead.



        Hope this helps!






        share|improve this answer

























          1












          1








          1







          Configure method is defined in WebHostBuilderExtensions class in the Microsoft.AspNetCore.Hosting assembly and its signature is as follows:



          public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder,
          Action<IApplicationBuilder> configureApp)

          ...



          This means you must pass an action containing an IApplicationBuilder implementation instance. Something similar you can find in the Startup class in the API you're testing.



          public void Configure(IApplicationBuilder app)




          Other dependencies can also be injected in the Configure method, provided they're registered with the DI container you're using. Configure() is called by the runtime so you normally don't need to explicitly invoke it.



          If what you need is to get an instance of your configuration in the Configure method, you can get it (or any other registered dependency) from the IServiceProvider instance that is exposed by the IApplicationBuilder object of the configuring action you pass to the method:



          Server = new TestServer(new WebHostBuilder()
          .Configure(appBuilder =>

          //get the required service from the appBuilder's ApplicationServices property
          var config = appBuilder.ApplicationServices.GetService<IConfiguration>();

          var section = config.GetSection("YourSection");
          //Use your section here.
          )
          .UseStartup<TestStartup>()


          However, please be careful because at that point it might be already too late to setup a service. Configure() is mostly used to setup the request pipeline. Even though you might have a legit use case to use the configuration in it, you should verify you can't do it in the ConfigureServices() method instead.



          Hope this helps!






          share|improve this answer













          Configure method is defined in WebHostBuilderExtensions class in the Microsoft.AspNetCore.Hosting assembly and its signature is as follows:



          public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder,
          Action<IApplicationBuilder> configureApp)

          ...



          This means you must pass an action containing an IApplicationBuilder implementation instance. Something similar you can find in the Startup class in the API you're testing.



          public void Configure(IApplicationBuilder app)




          Other dependencies can also be injected in the Configure method, provided they're registered with the DI container you're using. Configure() is called by the runtime so you normally don't need to explicitly invoke it.



          If what you need is to get an instance of your configuration in the Configure method, you can get it (or any other registered dependency) from the IServiceProvider instance that is exposed by the IApplicationBuilder object of the configuring action you pass to the method:



          Server = new TestServer(new WebHostBuilder()
          .Configure(appBuilder =>

          //get the required service from the appBuilder's ApplicationServices property
          var config = appBuilder.ApplicationServices.GetService<IConfiguration>();

          var section = config.GetSection("YourSection");
          //Use your section here.
          )
          .UseStartup<TestStartup>()


          However, please be careful because at that point it might be already too late to setup a service. Configure() is mostly used to setup the request pipeline. Even though you might have a legit use case to use the configuration in it, you should verify you can't do it in the ConfigureServices() method instead.



          Hope this helps!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 14:22









          Karel TamayoKarel Tamayo

          2,81721623




          2,81721623























              0














              Very thanks for your response:



              Other thing is I needed to add a reference to the Nuget:



              Microsoft.Extensions.Options.ConfigurationExtensions


              Now I can do something like:



              Server = new TestServer(
              new WebHostBuilder()
              .ConfigureServices(services =>


              services.Configure<SqlServerData>(Configuration.GetSection("SqlServerData"));
              services.Configure<SolrData>(Configuration.GetSection("SolrData"));
              )
              .UseStartup<TestStartup>()
              );


              Thanks






              share|improve this answer



























                0














                Very thanks for your response:



                Other thing is I needed to add a reference to the Nuget:



                Microsoft.Extensions.Options.ConfigurationExtensions


                Now I can do something like:



                Server = new TestServer(
                new WebHostBuilder()
                .ConfigureServices(services =>


                services.Configure<SqlServerData>(Configuration.GetSection("SqlServerData"));
                services.Configure<SolrData>(Configuration.GetSection("SolrData"));
                )
                .UseStartup<TestStartup>()
                );


                Thanks






                share|improve this answer

























                  0












                  0








                  0







                  Very thanks for your response:



                  Other thing is I needed to add a reference to the Nuget:



                  Microsoft.Extensions.Options.ConfigurationExtensions


                  Now I can do something like:



                  Server = new TestServer(
                  new WebHostBuilder()
                  .ConfigureServices(services =>


                  services.Configure<SqlServerData>(Configuration.GetSection("SqlServerData"));
                  services.Configure<SolrData>(Configuration.GetSection("SolrData"));
                  )
                  .UseStartup<TestStartup>()
                  );


                  Thanks






                  share|improve this answer













                  Very thanks for your response:



                  Other thing is I needed to add a reference to the Nuget:



                  Microsoft.Extensions.Options.ConfigurationExtensions


                  Now I can do something like:



                  Server = new TestServer(
                  new WebHostBuilder()
                  .ConfigureServices(services =>


                  services.Configure<SqlServerData>(Configuration.GetSection("SqlServerData"));
                  services.Configure<SolrData>(Configuration.GetSection("SolrData"));
                  )
                  .UseStartup<TestStartup>()
                  );


                  Thanks







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 14:27









                  Vicente MorenoVicente Moreno

                  132




                  132



























                      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%2f55064480%2ftyped-configuration-in-functional-test-whit-xunit-c-sharp%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

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко