"Test-infrastructure-fixes"

This commit is contained in:
sysadmin 2025-09-29 08:03:36 +01:00
parent 6e5f5ae279
commit c5c56ec3d8
2 changed files with 12 additions and 6 deletions

View File

@ -27,19 +27,25 @@ public class TestWebApplicationFactory : WebApplicationFactory<Program>
builder.ConfigureServices(services =>
{
// Remove the existing DbContext registration
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LittleShopContext>));
if (descriptor != null)
services.Remove(descriptor);
var contextDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(LittleShopContext));
if (contextDescriptor != null)
services.Remove(contextDescriptor);
// Add InMemory database for testing
var optionsDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LittleShopContext>));
if (optionsDescriptor != null)
services.Remove(optionsDescriptor);
// Add InMemory database for testing with unique name per test run
var databaseName = $"InMemoryDbForTesting_{Guid.NewGuid()}";
services.AddDbContext<LittleShopContext>(options =>
options.UseInMemoryDatabase("InMemoryDbForTesting")
options.UseInMemoryDatabase(databaseName)
.ConfigureWarnings(warnings => warnings.Default(WarningBehavior.Ignore)));
// Add test configuration
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{"ConnectionStrings:DefaultConnection", $"Data Source={databaseName}.db"},
{"Jwt:Key", "test-key-that-is-at-least-32-characters-long-for-security"},
{"Jwt:Issuer", "LittleShop"},
{"Jwt:Audience", "LittleShop"},

View File

@ -18,7 +18,7 @@ public class AdminPanelTests : IClassFixture<TestWebApplicationFactory>, IAsyncL
public AdminPanelTests(TestWebApplicationFactory factory)
{
_factory = factory;
_baseUrl = "https://localhost:5001"; // Adjust based on your test configuration
_baseUrl = _factory.Server.BaseAddress.ToString().TrimEnd('/'); // Use the test server's URL
}
public async Task InitializeAsync()