44using System . Threading . Tasks ;
55using CleanArchitecture . Blazor . Application . Common . Interfaces ;
66using CleanArchitecture . Blazor . Application . Common . Interfaces . Identity ;
7- using CleanArchitecture . Blazor . Application . Common . Interfaces . MediatorWrapper ;
8- using CleanArchitecture . Blazor . Application . Common . Interfaces . MultiTenant ;
97using CleanArchitecture . Blazor . Domain . Identity ;
108using CleanArchitecture . Blazor . Infrastructure ;
11- using CleanArchitecture . Blazor . Infrastructure . Extensions ;
9+ using CleanArchitecture . Blazor . Application . Common . Extensions ;
1210using CleanArchitecture . Blazor . Infrastructure . Persistence ;
1311using MediatR ;
1412using Microsoft . AspNetCore . Hosting ;
2018using NUnit . Framework ;
2119using Respawn ;
2220using Respawn . Graph ;
21+ using CleanArchitecture . Blazor . Application . Features . PicklistSets . DTOs ;
22+ using CleanArchitecture . Blazor . Infrastructure . Services ;
23+ using CleanArchitecture . Blazor . Application . Features . Tenants . DTOs ;
24+ using CleanArchitecture . Blazor . Infrastructure . Services . MultiTenant ;
25+ using System . Data . Common ;
26+ using CleanArchitecture . Blazor . Application . Common . Interfaces . MultiTenant ;
2327
2428namespace CleanArchitecture . Blazor . Application . IntegrationTests ;
2529
@@ -30,7 +34,6 @@ public class Testing
3034 private static IServiceScopeFactory _scopeFactory ;
3135 private static Respawner _checkpoint ;
3236 private static string _currentUserId ;
33- private static string _currentTenantId ;
3437
3538 [ OneTimeSetUp ]
3639 public async Task RunBeforeAnyTests ( )
@@ -46,7 +49,6 @@ public async Task RunBeforeAnyTests()
4649
4750 var services = new ServiceCollection ( ) ;
4851
49-
5052 services . AddSingleton ( Mock . Of < IWebHostEnvironment > ( w =>
5153 w . EnvironmentName == "Development" &&
5254 w . ApplicationName == "Server.UI" ) ) ;
@@ -58,44 +60,60 @@ public async Task RunBeforeAnyTests()
5860
5961 //startup.ConfigureServices(services);
6062
61- // Replace service registration for ICurrentUserService
62- // Remove existing registration
63- var currentUserServiceDescriptor = services . FirstOrDefault ( d =>
63+ // 替换 IUserContextAccessor 的注册
64+ var userContextServiceDescriptor = services . FirstOrDefault ( d =>
6465 d . ServiceType == typeof ( ICurrentUserAccessor ) ) ;
66+ if ( userContextServiceDescriptor != null )
67+ {
68+ services . Remove ( userContextServiceDescriptor ) ;
69+ }
6570
66- services . Remove ( currentUserServiceDescriptor ) ;
67-
68- // Register testing version
69- services . AddScoped ( provider =>
70- Mock . Of < ICurrentUserAccessor > ( s => s . SessionInfo . UserId == _currentUserId ) ) ;
71-
72-
73- _scopeFactory = services . BuildServiceProvider ( ) . GetService < IServiceScopeFactory > ( ) ;
74-
75- _checkpoint = await Respawner . CreateAsync ( _configuration . GetValue < string > ( "DatabaseSettings:ConnectionString" ) ,
76- new RespawnerOptions
71+ // 使用 Moq 创建 Mock 对象并配置 Current 属性
72+ services . AddSingleton < ICurrentUserAccessor > ( provider =>
73+ {
74+ var mockUserContextAccessor = new Mock < ICurrentUserAccessor > ( ) ;
75+ if ( ! string . IsNullOrEmpty ( _currentUserId ) )
7776 {
78- TablesToIgnore = new Table [ ] { "__EFMigrationsHistory" }
79- } ) ;
77+ var userContext = new SessionInfo ( "admin" , "admin" , "admin" , null , "" , "" , UserPresence . Available ) ;
78+ mockUserContextAccessor . Setup ( x => x . SessionInfo ) . Returns ( userContext ) ;
79+ }
80+ return mockUserContextAccessor . Object ;
81+ } ) ;
8082
83+ _scopeFactory = services . BuildServiceProvider ( ) . GetService < IServiceScopeFactory > ( ) ;
8184 EnsureDatabase ( ) ;
85+ using var scope = services . BuildServiceProvider ( ) . CreateScope ( ) ;
86+ var context = scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
87+ var connection = context . Database . GetDbConnection ( ) ;
88+ await connection . OpenAsync ( ) ;
89+ try
90+ {
91+ _checkpoint = await Respawner . CreateAsync (
92+ connection ,
93+ new RespawnerOptions
94+ {
95+ TablesToIgnore = new Table [ ] { "__EFMigrationsHistory" }
96+ } ) ;
97+ }
98+ finally
99+ {
100+ await connection . CloseAsync ( ) ;
101+ }
102+
103+
82104 }
83105
84106 private static void EnsureDatabase ( )
85107 {
86108 using var scope = _scopeFactory . CreateScope ( ) ;
87-
88109 var context = scope . ServiceProvider . GetService < ApplicationDbContext > ( ) ;
89-
90110 context . Database . Migrate ( ) ;
91111 }
92112
93113 public static async Task < TResponse > SendAsync < TResponse > ( IRequest < TResponse > request )
94114 {
95115 using var scope = _scopeFactory . CreateScope ( ) ;
96-
97- var mediator = scope . ServiceProvider . GetService < IScopedMediator > ( ) ;
98-
116+ var mediator = scope . ServiceProvider . GetService < IMediator > ( ) ;
99117 return await mediator . Send ( request ) ;
100118 }
101119
@@ -112,69 +130,73 @@ public static async Task<string> RunAsAdministratorAsync()
112130 public static async Task < string > RunAsUserAsync ( string userName , string password , string [ ] roles )
113131 {
114132 using var scope = _scopeFactory . CreateScope ( ) ;
115-
116133 var userManager = scope . ServiceProvider . GetService < UserManager < ApplicationUser > > ( ) ;
117-
118134 var user = new ApplicationUser { UserName = userName , Email = userName } ;
119-
120135 var result = await userManager . CreateAsync ( user , password ) ;
121136
122137 if ( roles . Any ( ) )
123138 {
124139 var roleManager = scope . ServiceProvider . GetService < RoleManager < IdentityRole > > ( ) ;
125-
126- foreach ( var role in roles ) await roleManager . CreateAsync ( new IdentityRole ( role ) ) ;
127-
140+ foreach ( var role in roles )
141+ {
142+ await roleManager . CreateAsync ( new IdentityRole ( role ) ) ;
143+ }
128144 await userManager . AddToRolesAsync ( user , roles ) ;
129145 }
130146
131147 if ( result . Succeeded )
132148 {
133149 _currentUserId = user . Id ;
134-
135150 return _currentUserId ;
136151 }
137152
138- var errors = string . Join ( Environment . NewLine , result . ToApplicationResult ( ) . Errors ) ;
139-
153+ var errors = string . Join ( Environment . NewLine , result . Errors ) ;
140154 throw new Exception ( $ "Unable to create { userName } .{ Environment . NewLine } { errors } ") ;
141155 }
142156
143157 public static async Task ResetState ( )
144158 {
145- await _checkpoint . ResetAsync ( _configuration . GetValue < string > ( "DatabaseSettings:ConnectionString" ) ) ;
159+ using var scope = _scopeFactory . CreateScope ( ) ;
160+ var context = scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
161+ var connection = context . Database . GetDbConnection ( ) ;
162+ await connection . OpenAsync ( ) ;
163+ try
164+ {
165+ await _checkpoint . ResetAsync ( connection ) ;
166+ }
167+ finally
168+ {
169+ await connection . CloseAsync ( ) ;
170+ }
146171 _currentUserId = null ;
147- _currentTenantId = null ;
148172 }
149173
150174 public static async Task < TEntity > FindAsync < TEntity > ( params object [ ] keyValues )
151175 where TEntity : class
152176 {
153177 using var scope = _scopeFactory . CreateScope ( ) ;
154-
155178 var context = scope . ServiceProvider . GetService < ApplicationDbContext > ( ) ;
156-
157179 return await context . FindAsync < TEntity > ( keyValues ) ;
158180 }
181+ public static IApplicationDbContext CreateDbContext ( )
182+ {
183+ var scope = _scopeFactory . CreateScope ( ) ;
184+ return scope . ServiceProvider . GetRequiredService < ApplicationDbContext > ( ) ;
185+ }
159186
160187 public static async Task AddAsync < TEntity > ( TEntity entity )
161188 where TEntity : class
162189 {
163190 using var scope = _scopeFactory . CreateScope ( ) ;
164-
165191 var context = scope . ServiceProvider . GetService < ApplicationDbContext > ( ) ;
166-
167192 context . Add ( entity ) ;
168-
169193 await context . SaveChangesAsync ( ) ;
170194 }
171195
172196 public static async Task < int > CountAsync < TEntity > ( ) where TEntity : class
173197 {
174198 using var scope = _scopeFactory . CreateScope ( ) ;
175-
176199 var context = scope . ServiceProvider . GetService < ApplicationDbContext > ( ) ;
177-
178200 return await context . Set < TEntity > ( ) . CountAsync ( ) ;
179201 }
180202
@@ -194,4 +216,4 @@ public static ITenantService CreateTenantsService()
194216 public void RunAfterAnyTests ( )
195217 {
196218 }
197- }
219+ }
0 commit comments