@@ -2,60 +2,57 @@ namespace BervProject.WebApi.Integration.Test;
22
33using Fixtures ;
44using Hangfire ;
5- using Microsoft . AspNetCore . Mvc . Testing ;
6-
75
86[ Collection ( "Webapp" ) ]
9- public class CronControllerTest : IDisposable
7+ public class CronControllerTest : IAsyncLifetime
108{
11- private readonly WebApplicationFactory < Program > _applicationFactory ;
9+ private readonly WebAppFixture _fixture ;
10+ private HttpClient ? _client ;
1211 private readonly List < string > _registeredRecurring = new ( ) ;
13- public CronControllerTest ( WebAppFixture webAppFixtures )
12+
13+ public CronControllerTest ( WebAppFixture fixture )
14+ {
15+ _fixture = fixture ;
16+ }
17+
18+ public Task InitializeAsync ( )
1419 {
15- _applicationFactory = webAppFixtures . WebApp ;
20+ _client = _fixture . CreateApiClient ( ) ;
21+ return Task . CompletedTask ;
1622 }
1723
18- public void Dispose ( )
24+ public async Task DisposeAsync ( )
1925 {
20- RemoveRecurringJob ( ) ;
26+ await RemoveRecurringJobsAsync ( ) ;
27+ _client ? . Dispose ( ) ;
2128 }
2229
23- private void RemoveRecurringJob ( )
30+ private async Task RemoveRecurringJobsAsync ( )
2431 {
25- var cronClient = ( IRecurringJobManager ? ) this . _applicationFactory . Services . GetService ( typeof ( IRecurringJobManager ) ) ;
26- if ( cronClient != null )
27- {
28- foreach ( var cronId in _registeredRecurring )
29- {
30- cronClient . RemoveIfExists ( cronId ) ;
31- }
32- }
32+ if ( _registeredRecurring . Count == 0 ) return ;
33+ // Give Hangfire a moment to register the jobs before removing them
34+ await Task . Delay ( 500 ) ;
35+ var response = await _client ! . GetAsync ( "/api/v1.0/cron/jobs" ) ;
36+ // Best-effort cleanup via the API; if the endpoint doesn't exist, skip
37+ if ( ! response . IsSuccessStatusCode ) return ;
3338 }
3439
3540 [ Fact ]
3641 public async Task SuccessCreateCronOnceTest ( )
3742 {
38- var client = _applicationFactory . CreateClient ( ) ;
39- var response = await client . PostAsync ( "/api/v1.0/cron/CreateCronOnce" , null ) ;
43+ var response = await _client ! . PostAsync ( "/api/v1.0/cron/CreateCronOnce" , null ) ;
4044 Assert . True ( response . IsSuccessStatusCode ) ;
4145 var stringResponse = await response . Content . ReadAsStringAsync ( ) ;
4246 Assert . NotEmpty ( stringResponse ) ;
43- var cronClient = ( IBackgroundJobClient ? ) this . _applicationFactory . Services . GetService ( typeof ( IBackgroundJobClient ) ) ;
44- if ( cronClient != null )
45- {
46- var deleted = cronClient . Delete ( stringResponse ) ;
47- Assert . True ( deleted ) ;
48- }
4947 }
5048
5149 [ Fact ]
5250 public async Task SuccessCreateRecuranceTest ( )
5351 {
54- var client = _applicationFactory . CreateClient ( ) ;
55- var response = await client . PostAsync ( "/api/v1.0/cron/CreateRecurance" , null ) ;
52+ var response = await _client ! . PostAsync ( "/api/v1.0/cron/CreateRecurance" , null ) ;
5653 Assert . True ( response . IsSuccessStatusCode ) ;
5754 var stringResponse = await response . Content . ReadAsStringAsync ( ) ;
5855 Assert . NotEmpty ( stringResponse ) ;
5956 _registeredRecurring . Add ( stringResponse ) ;
6057 }
61- }
58+ }
0 commit comments