I am using .NET Core 2.2 IHostBuilder (Generic Host Builder) to build a console app running message streaming app as a BackgroundService (IHostedService). https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2
This is not the IWebHostBuilder that is typically used for hosting an ASP.NET Core web app.
I'd like to build some basic integration test to validate graceful shutdowns and the IoC configuration. I want to start a TestServer that hosts my IHostBuilder host. I don't need to serve any kinds of request.
I've done this kind of thing with IWebHostBuilder and TestServer in Microsoft.AspNetCore.TestHost. However, there is no support for IHostBuilder, only IWebHostBuilder. https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver.-ctor?view=aspnetcore-2.2#Microsoft_AspNetCore_TestHost_TestServer__ctor_Microsoft_AspNetCore_Hosting_IWebHostBuilder_
I know IHostBuilder will replace IWebHostBuilder in .NET Core 3.0 and TestServer there will support this. Trying to find a way to do this in .NET Core 2.2.
RunAsync()
and test whatever you want to test. The only disadvantage is you need to repeat all your DI registration and configuration in test application – OlegIIRepository<>
) and all controllers. It should be sufficient. And you don't need to run the whole application for that. – Vladimir Serykh