Skip to content

Commit d3d1faf

Browse files
committed
updating integration test to use the same standard
1 parent fa28523 commit d3d1faf

19 files changed

+139
-87
lines changed

api.Tests/ApiRoutesTests/CategoryNaturalAreaApiIntegrationTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
namespace api.Tests.ApiRoutesTests;
55

6-
public class CategoryNaturalAreaApiIntegrationTests : IClassFixture<CustomWebApplicationFactory> , IDisposable
6+
public class CategoryNaturalAreaApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
77
{
8-
private readonly HttpClient _client = new CustomWebApplicationFactory().CreateClient();
8+
private readonly HttpClient _client;
99

10-
public void Dispose()
10+
public CategoryNaturalAreaApiIntegrationTests(CustomWebApplicationFactory factory)
1111
{
12-
_client.Dispose();
12+
factory.ResetDatabase();
13+
_client = factory.CreateClient();
1314
}
1415

1516
[Fact]

api.Tests/ApiRoutesTests/CityApiIntegrationTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44

55
namespace api.Tests.ApiRoutesTests;
66

7-
public class CityApiIntegrationTests : IClassFixture<CustomWebApplicationFactory> , IDisposable
7+
public class CityApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
88
{
99
private readonly HttpClient _client;
1010

1111
public CityApiIntegrationTests(CustomWebApplicationFactory factory)
1212
{
13-
_client = new CustomWebApplicationFactory().CreateClient();
14-
}
15-
16-
public void Dispose()
17-
{
18-
_client.Dispose();
13+
factory.ResetDatabase();
14+
_client = factory.CreateClient();
1915
}
2016

2117
[Fact]

api.Tests/ApiRoutesTests/ConstitutionArticleApiIntegrationTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace api.Tests.ApiRoutesTests;
66

7-
public class ConstitutionArticleApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>, IDisposable
7+
public class ConstitutionArticleApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
88
{
9-
private readonly HttpClient _client = new CustomWebApplicationFactory().CreateClient();
9+
private readonly HttpClient _client;
1010

11-
public void Dispose()
11+
public ConstitutionArticleApiIntegrationTests(CustomWebApplicationFactory factory)
1212
{
13-
_client.Dispose();
13+
factory.ResetDatabase();
14+
_client = factory.CreateClient();
1415
}
1516

1617

api.Tests/ApiRoutesTests/CountryApiIntegrationTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
namespace api.Tests.ApiRoutesTests;
55

6-
public class CountryApiIntegrationTests(CustomWebApplicationFactory factory)
7-
: IClassFixture<CustomWebApplicationFactory>
6+
public class CountryApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
87
{
9-
private readonly HttpClient _client = factory.CreateClient();
8+
private readonly HttpClient _client;
9+
10+
public CountryApiIntegrationTests(CustomWebApplicationFactory factory)
11+
{
12+
factory.ResetDatabase();
13+
_client = factory.CreateClient();
14+
}
1015

1116
[Fact]
1217
public async Task GetCountry_ReturnsAllCountryData()

api.Tests/ApiRoutesTests/CustomWebApplicationFactory.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Mvc.Testing;
55
using Microsoft.EntityFrameworkCore;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.DependencyInjection.Extensions;
78

89
namespace api.Tests.ApiRoutesTests;
910

@@ -12,38 +13,28 @@ public class CustomWebApplicationFactory : WebApplicationFactory<Program>
1213
private readonly string _databaseName = $"TestDatabase_{Guid.NewGuid()}";
1314

1415
// Generate a unique database name for each instance
15-
16-
1716
protected override void ConfigureWebHost(IWebHostBuilder builder)
1817
{
1918
builder.ConfigureServices(services =>
2019
{
21-
var descriptor = services.SingleOrDefault(
22-
d => d.ServiceType == typeof(DbContextOptions<DBContext>));
23-
if (descriptor != null)
24-
{
25-
services.Remove(descriptor);
26-
}
27-
20+
// 1. Remove the production DbContext registration
21+
services.RemoveAll(typeof(DbContextOptions<DBContext>));
22+
23+
// 2. Create a NEW internal service provider for EF Core
24+
// This is the key to preventing the provider conflict
25+
var efInternalServiceProvider = new ServiceCollection()
26+
.AddEntityFrameworkInMemoryDatabase()
27+
.BuildServiceProvider();
28+
29+
// 3. Register the DbContext using the isolated provider
2830
services.AddDbContext<DBContext>(options =>
2931
{
3032
options.UseInMemoryDatabase(_databaseName);
33+
options.UseInternalServiceProvider(efInternalServiceProvider);
3134
});
32-
33-
var serviceProvider = services.BuildServiceProvider();
34-
using var scope = serviceProvider.CreateScope();
35-
var dbContext = scope.ServiceProvider.GetRequiredService<DBContext>();
36-
dbContext.Database.EnsureDeleted();
37-
dbContext.Database.EnsureCreated();
38-
39-
var fixture = new Fixture();
40-
41-
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
42-
fixture.Customize<DateOnly>(composer => composer.FromFactory(() => DateOnly.FromDateTime(DateTime.Now)));
43-
SeedDatabase(dbContext);
4435
});
4536
}
46-
37+
4738
private void SeedDatabase(DBContext dbContext)
4839
{
4940

@@ -549,6 +540,11 @@ public void ResetDatabase()
549540
var dbContext = scope.ServiceProvider.GetRequiredService<DBContext>();
550541
dbContext.Database.EnsureDeleted();
551542
dbContext.Database.EnsureCreated();
543+
var fixture = new Fixture();
544+
545+
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
546+
fixture.Customize<DateOnly>(composer => composer.FromFactory(() => DateOnly.FromDateTime(DateTime.Now)));
547+
552548
SeedDatabase(dbContext);
553549
}
554550
}

api.Tests/ApiRoutesTests/DepartmentEndpointTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace api.Tests.ApiRoutesTests;
66

7-
public class DepartmentApiIntegrationTests : IClassFixture<CustomWebApplicationFactory> , IDisposable
7+
public class DepartmentApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
88
{
9-
private readonly HttpClient _client = new CustomWebApplicationFactory().CreateClient();
9+
private readonly HttpClient _client;
1010

11-
public void Dispose()
11+
public DepartmentApiIntegrationTests(CustomWebApplicationFactory factory)
1212
{
13-
_client.Dispose();
13+
factory.ResetDatabase();
14+
_client = factory.CreateClient();
1415
}
1516

1617
// BadRequest scenarios

api.Tests/ApiRoutesTests/HolidayApiIntegrationTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44

55
namespace api.Tests.ApiRoutesTests;
66

7-
public class HolidayApiIntegrationTests(CustomWebApplicationFactory factory) : IClassFixture<CustomWebApplicationFactory>
7+
public class HolidayApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
88
{
9-
private readonly HttpClient _client = factory.CreateClient();
9+
private readonly HttpClient _client;
10+
11+
public HolidayApiIntegrationTests(CustomWebApplicationFactory factory)
12+
{
13+
factory.ResetDatabase();
14+
_client = factory.CreateClient();
15+
}
1016

1117
[Theory]
1218
[InlineData(2025)]

api.Tests/ApiRoutesTests/IndigenousReservationApiIntegrationTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55

66
namespace api.Tests.ApiRoutesTests;
77

8-
public class IndigenousReservationApiIntegrationTests(CustomWebApplicationFactory factory)
9-
: IClassFixture<CustomWebApplicationFactory>
8+
public class IndigenousReservationApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
109
{
11-
private readonly HttpClient _client = factory.CreateClient();
10+
private readonly HttpClient _client;
11+
12+
public IndigenousReservationApiIntegrationTests(CustomWebApplicationFactory factory)
13+
{
14+
factory.ResetDatabase();
15+
_client = factory.CreateClient();
16+
}
1217

1318
[Fact]
1419
public async Task GetIndigenousReservations_ReturnsOkWithExpectedData()

api.Tests/ApiRoutesTests/InfoApiIntegrationTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace api.Tests.ApiRoutesTests;
44

5-
public class InfoApiIntegrationTests(CustomWebApplicationFactory factory) : IClassFixture<CustomWebApplicationFactory>
5+
public class InfoApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
66
{
7-
private readonly HttpClient _client = factory.CreateClient();
7+
private readonly HttpClient _client;
8+
9+
public InfoApiIntegrationTests(CustomWebApplicationFactory factory)
10+
{
11+
factory.ResetDatabase();
12+
_client = factory.CreateClient();
13+
}
814

915
#if DEBUG
1016
[Fact]

api.Tests/ApiRoutesTests/InvasiveSpecieApiIntegrationTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
namespace api.Tests.ApiRoutesTests;
66

7-
public class InvasiveSpecieApiIntegrationTests(CustomWebApplicationFactory factory)
8-
: IClassFixture<CustomWebApplicationFactory>
7+
public class InvasiveSpecieApiIntegrationTests : IClassFixture<CustomWebApplicationFactory>
98
{
10-
private readonly HttpClient _client = factory.CreateClient();
9+
private readonly HttpClient _client;
10+
11+
public InvasiveSpecieApiIntegrationTests(CustomWebApplicationFactory factory)
12+
{
13+
factory.ResetDatabase();
14+
_client = factory.CreateClient();
15+
}
1116

1217
[Fact]
1318
public async Task GetInvasiveSpecies_ReturnsOkWithExpectedData()

0 commit comments

Comments
 (0)