Skip to content

Commit 12954e9

Browse files
fixing test failures
1 parent d5a07b2 commit 12954e9

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

ECommerceApp.IntegrationTests/CustomWebApplicationFactory.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99

1010
namespace ECommerceApp.IntegrationTests;
1111

12+
// Separate static class for database names to avoid static field issues in generic class
13+
internal static class InMemoryDatabaseNames
14+
{
15+
public static readonly string ProductsDbName = $"InMemoryProductsTestDb_{Guid.NewGuid()}";
16+
public static readonly string CustomersDbName = $"InMemoryCustomersTestDb_{Guid.NewGuid()}";
17+
public static readonly string OrdersDbName = $"InMemoryOrdersTestDb_{Guid.NewGuid()}";
18+
public static readonly string BusinessEventsDbName = $"InMemoryBusinessEventsTestDb_{Guid.NewGuid()}";
19+
}
20+
1221
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup>
1322
where TStartup : class
1423
{
1524
private readonly string _connectionString;
16-
// Create static names for in-memory databases to ensure they persist across requests within the same test
17-
private static readonly string ProductsDbName = $"InMemoryProductsTestDb_{Guid.NewGuid()}";
18-
private static readonly string CustomersDbName = $"InMemoryCustomersTestDb_{Guid.NewGuid()}";
19-
private static readonly string OrdersDbName = $"InMemoryOrdersTestDb_{Guid.NewGuid()}";
20-
private static readonly string BusinessEventsDbName = $"InMemoryBusinessEventsTestDb_{Guid.NewGuid()}";
2125

2226
public CustomWebApplicationFactory(string connectionString)
2327
{
@@ -38,7 +42,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
3842
// Add in-memory database for ProductDbContext
3943
services.AddDbContext<ECommerce.Modules.Products.Persistence.ProductDbContext>(options =>
4044
{
41-
options.UseInMemoryDatabase(ProductsDbName);
45+
options.UseInMemoryDatabase(InMemoryDatabaseNames.ProductsDbName);
4246
});
4347

4448
// Remove and add in-memory for CustomerDbContext
@@ -50,7 +54,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
5054
}
5155
services.AddDbContext<ECommerce.Modules.Customers.Persistence.CustomerDbContext>(options =>
5256
{
53-
options.UseInMemoryDatabase(CustomersDbName);
57+
options.UseInMemoryDatabase(InMemoryDatabaseNames.CustomersDbName);
5458
});
5559

5660
// Remove and add in-memory for OrderDbContext
@@ -62,7 +66,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
6266
}
6367
services.AddDbContext<ECommerce.Modules.Orders.Persistence.OrderDbContext>(options =>
6468
{
65-
options.UseInMemoryDatabase(OrdersDbName);
69+
options.UseInMemoryDatabase(InMemoryDatabaseNames.OrdersDbName);
6670
});
6771

6872
// Remove and add in-memory for BusinessEventDbContext
@@ -74,10 +78,11 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
7478
}
7579
services.AddDbContext<ECommerce.BusinessEvents.Persistence.BusinessEventDbContext>(options =>
7680
{
77-
options.UseInMemoryDatabase(BusinessEventsDbName);
81+
options.UseInMemoryDatabase(InMemoryDatabaseNames.BusinessEventsDbName);
7882
});
7983
});
80-
builder.ConfigureAppConfiguration((context, configBuilder) =>
84+
85+
builder.ConfigureAppConfiguration((_, configBuilder) =>
8186
{
8287
var inMemorySettings = new Dictionary<string, string>
8388
{

0 commit comments

Comments
 (0)