Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration test and hosting ASP.NET Core 6.0 without Startup class

To setup unit tests in previous versions of .Net Core, I could host my WebApp or WebAPI in a test project the following way:

         IHost host = Host.CreateDefaultBuilder()
            .ConfigureWebHostDefaults(config =>
            {
                config.UseStartup<MyWebApp.Startup>();
                config.UseUrls("https://localhost:44331/");
                ...    
            })
            .Build();

The current .Net 6.0 does not use Startup class concept, and thus it could not be referenced. How can host AspNet apps in a test project in a proper and clean way?

like image 934
user2341923 Avatar asked Nov 24 '21 09:11

user2341923


People also ask

Is startup class mandatory in ASP.NET Core?

Is Startup Class mandatory? Yes, this class is mandatory in ASP.net core application. It can have any access modifier (public, private, internal). This is the entry point of the ASP.net application.

What is WebApplicationFactory?

WebApplicationFactory<TEntryPoint>() Creates an instance of WebApplicationFactory<TEntryPoint>. This factory can be used to create a TestServer instance using the MVC application defined by TEntryPoint and one or more HttpClient instances used to send HttpRequestMessage to the TestServer.

What is integration test in ASP NET Core?

Thank you. Integration tests ensure that an app's components function correctly at a level that includes the app's supporting infrastructure, such as the database, file system, and network. ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.

Do I need a custom startup class for integration tests?

My previous post demonstrated how to use a custom appsettings.js file with integration tests in ASP.NET Core. But in practice it's not enough and very often we need a custom startup class that extends the one in the web application project to configure the application for integration tests. This blog post shows how to do this.

What is MVC testing in ASP NET Core?

The Microsoft.AspNetCore.Mvc.Testing package handles the following tasks: Copies the dependencies file (*.deps) from the SUT into the test project's bin directory. Sets the content root to the SUT's project root so that static files and pages/views are found when the tests are executed.

How to make test host use fake startup class with web application?

To make test host use our fake startup class with web application we need to apply some magic. First we need custom web application factory that provides integration tests mechanism with custom web host builder. Here is my application factory – dummy and very general.


1 Answers

Note that you can switch to generic hosting model (the one using the startup class) if you want.

To set up integration tests with the new minimal hosting model you can make web project internals visible to the test one for example by adding next property to csproj:

<ItemGroup>
  <InternalsVisibleTo Include ="YourTestProjectName"/>
</ItemGroup>

And then you can use the Program class generated for the web app in WebApplicationFactory:

class MyWebApplication : WebApplicationFactory<Program>
{
    protected override IHost CreateHost(IHostBuilder builder)
    {
        // shared extra set up goes here
        return base.CreateHost(builder);
    }
}

And then in the test:

var application = new MyWebApplication();
var client = application.CreateClient();
var response = await client.GetStringAsync("/api/WeatherForecast");

Or use WebApplicationFactory<Program> from the test directly:

var application = new WebApplicationFactory<Program>()
.WithWebHostBuilder(builder =>
{
    builder.ConfigureServices(services =>
    {
       // set up servises
    });
});
var client = application.CreateClient();
var response = await client.GetStringAsync("/api/WeatherForecast");

Or instead of using InternalsVisibleTo you can declare public partial Program class and use it. For example add next to the bottom of top-level statement file (the rest is the same):

public partial class Program { }

Code examples from migration guide.

like image 123
Guru Stron Avatar answered Oct 12 '22 13:10

Guru Stron