1919using JetBrains . Annotations ;
2020using Microsoft . EntityFrameworkCore ;
2121using Microsoft . Extensions . DependencyInjection ;
22+ using ReactReduxTodo . Tests . Integration . Models ;
2223using Thread = Hikkaba . Data . Entities . Thread ;
2324
2425namespace Hikkaba . Tests . Integration . Tests . Repositories ;
@@ -44,10 +45,24 @@ public async Task OneTimeTearDownAsync()
4445 }
4546
4647 [ MustDisposeResource ]
47- private async Task < CustomAppFactory > CreateAppFactoryAsync ( )
48+ private async Task < ISeedResult > Seed ( CancellationToken cancellationToken )
4849 {
4950 var connectionString = await _contextManager ! . CreateRespawnedDbConnectionStringAsync ( ) ;
50- return new CustomAppFactory ( connectionString ) ;
51+ var customAppFactory = new CustomAppFactory ( connectionString ) ;
52+
53+ var scope = customAppFactory . Services . GetRequiredService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
54+ var dbContext = scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
55+
56+ if ( ( await dbContext . Database . GetPendingMigrationsAsync ( cancellationToken ) ) . Any ( ) )
57+ {
58+ await dbContext . Database . MigrateAsync ( cancellationToken ) ;
59+ }
60+
61+ return new SeedResult
62+ {
63+ Scope = scope ,
64+ AppFactory = customAppFactory ,
65+ } ;
5166 }
5267
5368 [ CancelAfter ( TestDefaults . TestTimeout ) ]
@@ -61,16 +76,12 @@ public async Task ListBansPaginatedAsync_WhenSearchExact_ReturnsExpectedResult(
6176 CancellationToken cancellationToken )
6277 {
6378 // Arrange
64- await using var customAppFactory = await CreateAppFactoryAsync ( ) ;
65- using var scope = customAppFactory . Services . GetRequiredService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
66- var timeProvider = scope . ServiceProvider . GetRequiredService < TimeProvider > ( ) ;
67- var dbContext = scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
68- var hashService = scope . ServiceProvider . GetRequiredService < IHashService > ( ) ;
79+ using var seedResult = await Seed ( cancellationToken ) ;
80+ using var serviceScope = seedResult . Scope . ServiceProvider . GetRequiredService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
6981
70- if ( ( await dbContext . Database . GetPendingMigrationsAsync ( cancellationToken ) ) . Any ( ) )
71- {
72- await dbContext . Database . MigrateAsync ( cancellationToken ) ;
73- }
82+ var dbContext = serviceScope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
83+ var hashService = serviceScope . ServiceProvider . GetRequiredService < IHashService > ( ) ;
84+ var timeProvider = serviceScope . ServiceProvider . GetRequiredService < TimeProvider > ( ) ;
7485
7586 // Seed
7687 var admin = new ApplicationUser
@@ -168,7 +179,7 @@ public async Task ListBansPaginatedAsync_WhenSearchExact_ReturnsExpectedResult(
168179
169180 await dbContext . SaveChangesAsync ( cancellationToken ) ;
170181
171- var repository = scope . ServiceProvider . GetRequiredService < IBanRepository > ( ) ;
182+ var repository = serviceScope . ServiceProvider . GetRequiredService < IBanRepository > ( ) ;
172183
173184 // Act
174185 var result = await repository . ListBansPaginatedAsync ( new BanPagingFilter
@@ -195,16 +206,10 @@ public async Task ListBansPaginatedAsync_WhenSearchInRange_ReturnsExpectedResult
195206 CancellationToken cancellationToken )
196207 {
197208 // Arrange
198- await using var customAppFactory = await CreateAppFactoryAsync ( ) ;
199- using var scope = customAppFactory . Services . GetRequiredService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
200- var timeProvider = scope . ServiceProvider . GetRequiredService < TimeProvider > ( ) ;
201- var dbContext = scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
202- var hashService = scope . ServiceProvider . GetRequiredService < IHashService > ( ) ;
203-
204- if ( ( await dbContext . Database . GetPendingMigrationsAsync ( cancellationToken ) ) . Any ( ) )
205- {
206- await dbContext . Database . MigrateAsync ( cancellationToken ) ;
207- }
209+ using var seedResult = await Seed ( cancellationToken ) ;
210+ var dbContext = seedResult . Scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
211+ var hashService = seedResult . Scope . ServiceProvider . GetRequiredService < IHashService > ( ) ;
212+ var timeProvider = seedResult . Scope . ServiceProvider . GetRequiredService < TimeProvider > ( ) ;
208213
209214 // Seed
210215 var admin = new ApplicationUser
@@ -306,7 +311,7 @@ public async Task ListBansPaginatedAsync_WhenSearchInRange_ReturnsExpectedResult
306311
307312 await dbContext . SaveChangesAsync ( cancellationToken ) ;
308313
309- var repository = scope . ServiceProvider . GetRequiredService < IBanRepository > ( ) ;
314+ var repository = seedResult . Scope . ServiceProvider . GetRequiredService < IBanRepository > ( ) ;
310315
311316 // Act
312317 var result = await repository . ListBansPaginatedAsync ( new BanPagingFilter
0 commit comments