ASP.NET Core has WebApplicationFactory<T> that can be used to start up a test host for integration test purposes.
Is there an equivalent for a non ASP.NET Core project? I'm thinking of the ".NET Core Worker Service Template" for example. It doesn't have any references to ASP.NET Core, but it uses the generic host.
For example, I have a project like this one, hosted as a windows service. I would like to create integration tests as easily for this as I do for ASP.NET Core projects (where I can reuse DI from the Startup file, so I don't need to set that up in the test.
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddAppSettings(context.HostingEnvironment.EnvironmentName);
})
.ConfigureServices((hostContext, services) =>
{
// Loads of configuration (DI, Logging, Messaging, etc) removed for brevity
// ...
})
.UseWindowsService();
}
In this case the service listens for messages (using Masstransit) so there is no HostedService setup. But there might be as well in other cases.