My current project is .netcore 3.0 and i want to update in .net 8.0.
while updating integration test project WebApplicationFactory class gives error. I have also migrate My startup file into Program file.
below is my code for WebApplicationFactory.cs class
public class ServiceWorkerFactory<TProgram>
: WebApplicationFactory<TProgram> where TProgram : class
{
protected override IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<TProgram>();
}
)
.ConfigureAppConfiguration(
(
hostingContext,
config
) =>
{
config.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>(
"ConnectionStrings:test",
DatabaseTestSetup.test
),
});
});
}
protected override void ConfigureWebHost
(
IWebHostBuilder builder
)
{
base.ConfigureWebHost(builder);
builder.ConfigureServices((context, services) =>
{
var sp = services.BuildServiceProvider();
//Need to pass the tenant id dynamically probably from appconfig
//Get the integration test application settings
var integrationTestAppSettings = context.Configuration.GetSection("IntegrationTest").Get<IntegrationTestAppSettings>();
if (integrationTestAppSettings != null)
{
services.AddSingleton(integrationTestAppSettings);
}
else
{
throw new System.Exception("Integration testing appsetting configuration not found !!");
}
var tc = new TaskContext() { TenantId = integrationTestAppSettings.TenantId };
builder.ConfigureTestServices(services =>
{
services.AddHangfire(options => options.UseMemoryStorage());
// Mock external dependency
services.SwapService(Substitute.For<ITestService>());
});
}
}
I have tried remove CreateHostBuilder method and put that code in ConfigureWebHost method. that code will work but it will not consider my in memory config. it will take my appsettings configuration.
Please suggest me if i have done anything wrong