鼎鼎知识库
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

03CleanArchitecture下的CleanTesting.md 11KB

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. # 用到的组件
  2. 本篇涉及的测试形式:
  3. - `Subcutaneous`测试:最接近`UI`层的测试,聚焦在输入和输出,非常适合逻辑和UI分离的场景,这样就避免进行UI测试,容易写也容易维护
  4. - `Integration`集成测试:
  5. - `Unit`单元测试:
  6. ## `FluentAssertions`组件
  7. - 支持非常自然地规定自动测试的结果
  8. - 更好的可读性
  9. - 更好的测试失败说明
  10. - 避免调试
  11. - 更有生产力,更简单
  12. ## `Moq`组件
  13. - 为`.NET`而来
  14. - 流行并且友好
  15. - 支持`Mock`类和接口
  16. - 强类型,这样可以避免和系统关键字冲突
  17. - 简单易用
  18. ## `EF Core InMemory`组件
  19. - 内置数据库引擎
  20. - 内存数据库
  21. - 适用于测试场景
  22. - 轻量级无依赖
  23. ## `Respawn`组件
  24. - 为集成测试准备的数据库智能清洗
  25. - 避免删除数据或回滚事务
  26. - 把数据库恢复到一个干净的某个节点
  27. - 忽略表和`Schema`,比如忽略`_EFMigrationsHistory`
  28. - 支持`SQL Server`, `Postgres`,`MySQL`
  29. # 界面
  30. ## 列表
  31. ![t1](F:\SourceCodes\DDWiki\专题\后端\测试\t1.png)
  32. ## 删除和更新
  33. ![t2](F:\SourceCodes\DDWiki\专题\后端\测试\t2.png)
  34. # 解决方案组件依赖
  35. - `.NET Core Template Package`
  36. - `ASP.NET Core 3.1`
  37. - `Entity Framework Core 3.1`
  38. - `ASP.NET Core Identity 3.1`
  39. - `Angular 9`
  40. # 文件结构
  41. - `Application`层
  42. - `Domain`层
  43. - `Infrastructure`层
  44. - `WebUI`层
  45. - 集成测试层
  46. # `WebUI`层
  47. `TodoListsController`
  48. ```
  49. [Authorize]
  50. public class TodoListsController : ApiController
  51. {
  52. [HttpGet]
  53. public async Task<IActionResult<TodoVm>> Get()
  54. {
  55. return await Mediator.Send(new GetTodosQuery());
  56. }
  57. [HttpGet("{id}")]
  58. public async Task<FileResult> Get(int id)
  59. {
  60. var vm = await Mediator.Send(new ExportTodosQuery {ListId=id});
  61. return File(vm.Content, vm.ContentType, vm.FileName);
  62. }
  63. [HttpPost]
  64. public async Task<IActionResult<int>> Create(CreateTodoListCommand command)
  65. {
  66. return await Mediator.Send(command);
  67. }
  68. }
  69. ```
  70. # `Application`层
  71. `GetTodosQuery.cs`
  72. ```
  73. public class GetTodosQuery : IRequest<TodosVm>
  74. {
  75. }
  76. public class GetTodosQueryHandler : IRequestHandler<GetTodosQuery, TodosVm>
  77. {
  78. private readonly IApplicationDbContext _context;
  79. private readonly IMapper _mapper;
  80. public GetTodosQueryHandler(IApplicationDbContext context, IMapper mapper)
  81. {
  82. _context = context;
  83. _mapper = mapper;
  84. }
  85. public async Task<TodosVm> Handler(GetTodosQuery request, CancellationToken cancellationToken)
  86. {
  87. return new TodosVm{
  88. PriorityLevels = Enum.GetValues(typeof(PriorityLevel))
  89. .Cast<PriorityLevel>()
  90. .Select(p => new PriorityLevelDto{Value=(int)p, Name=p.ToString()})
  91. .ToList,
  92. Lists = await _context.TodosLists
  93. .ProjectTo<TodoListDto>(_mapper.ConfigurationProvider)
  94. .OrdreBy(t => t.Title)
  95. .ToListAsync(cacnellationToken);
  96. };
  97. }
  98. }
  99. ```
  100. 在`MediatR`的请求管道中做验证。
  101. ```
  102. public class RequestValidationBehavior<TRequest, TResponse> : IPiplelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
  103. {
  104. private readonly IENumerable<IValidator<TRequest>> _validators;
  105. public RequestValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
  106. {
  107. _validators = validators;
  108. }
  109. public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
  110. {
  111. if(_validators.Any())
  112. {
  113. var context = new ValidationContext(request);
  114. var failures = _validators
  115. .Select(v => v.Validate(context))
  116. .SelectMany(result => result.Errors)
  117. .Whre(f => f != null)
  118. .ToList();
  119. if(failures.Count != 0)
  120. {
  121. throw new ValidationException(failures);
  122. }
  123. }
  124. return next();
  125. }
  126. }
  127. ```
  128. 在`MediatR`的请求管道中处理异常。
  129. ```
  130. public class UnhandledExceptionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
  131. {
  132. private readonly ILogger<TRequest> _logger;
  133. public UnhandledExceptionBehaviour(ILogger<TRequest> logger)
  134. {
  135. _logger = logger;
  136. }
  137. public async Task<TResponse> Handle(TRequest request, CancellationTOken canellationToken, RequestHandlerDelegate<TResponse> next)
  138. {
  139. try
  140. {
  141. return await next();
  142. }
  143. catch(Exception ex)
  144. {
  145. var requestName = typeof(TRequest).Name;
  146. _logger.LogError();
  147. throw;
  148. }
  149. }
  150. }
  151. ```
  152. # 集成测试层
  153. 引用`WebUI`层
  154. ```
  155. dotnet add package FluentAssertions
  156. dotnet add package Moq
  157. dotnet add package Respawn
  158. ```
  159. `GetTodosTests.cs`
  160. ```
  161. using static Testing;
  162. public class GetTodosTests : TestBase //每次运行测试让数据库回到干净的状态
  163. {
  164. [Test]
  165. public async Task ShouldReturnAllListsAndAssociatedItems()
  166. {
  167. //Arrange
  168. await AddAsync(new TodoList{
  169. Title = "",
  170. Items = {
  171. new TodoItem {Title="",Done=true},
  172. new TodoItem {Title="",Done=true},
  173. new TodoItem {Title="",Done=true},
  174. new TodoItem {Title=""},
  175. new TodoItem {Title=""},
  176. new TodoItem {Title=""}
  177. }
  178. });
  179. var query = new GetTodosQuery();
  180. //Act
  181. TodosVm result = await SendAsync(query);
  182. //Assert
  183. result.Should().NotBeNull();
  184. result.List.Should().HaveCount(1);
  185. result.Lists.First().Items.Should().HaveCount();
  186. }
  187. }
  188. ```
  189. `CreateTodoListTests.cs`
  190. ```
  191. using static Testing;
  192. public class CreateTodListTests : TestBase//每次运行测试让数据库回到干净的状态
  193. {
  194. [Test]
  195. public void ShouldRequredMinimumFileds()
  196. {
  197. var command = new CrateTodoListCommand();
  198. FluenActions.Invoking(()=> SendAsync(command))
  199. .Should().Throw<ValidaitonException>();
  200. }
  201. [Test]
  202. public async Task ShouldRequiredUniqueTitle()
  203. {
  204. await SendAsync(new CreateTodoListCommand{
  205. Title = ""
  206. });
  207. var command = new CreateTodoListCommand
  208. {
  209. Title = ""
  210. };
  211. FluenActions.Invoking(() => SendAsync(comamnd))
  212. .Should().Throw<ValidationException>();
  213. }
  214. [Test]
  215. public async Task ShouldCreateTodoList()
  216. {
  217. var useId = await RunAsDefaultUserAsync();
  218. var command = new CreateTodoListCommand
  219. {
  220. Title = ""
  221. };
  222. var listeId = await SendAsync(command);
  223. var list = await FindAsync<TodoList>(listId);
  224. list.Should().NotBuNull();
  225. list.Title.Should().Be(command.Title);
  226. list.Created.Should().BeCloseTo(DateTime.Now, 10000);
  227. list.CreatedBy.Should().Be(userId);
  228. }
  229. }
  230. ```
  231. `UpdateDotListTests.cs`
  232. ```
  233. using static Testing;
  234. public class UpdateTodoListTests : TestBase
  235. {
  236. [Test]
  237. public void ShouldRequireValidTodoListId()
  238. {
  239. var command = new UpdateTodoListCommand
  240. {
  241. Id = 99,
  242. Title = "New Title"
  243. };
  244. FluenActions.Invoking(() => SendAsync(command)).Should().Throw<NotFoundException>();
  245. }
  246. [Test]
  247. public async Task ShouldRequireUniqueTitle()
  248. {
  249. var listId = await SendAsync(new CreateTodoListCommand{Title=""});
  250. await SendAsync(new CreateTodoListCommand={Title=""});
  251. var command = new UpdateTodoListCommand
  252. {
  253. Id = listId,
  254. Title = ""
  255. };
  256. FluentActions.Invoking(()=>
  257. SendAsync(command)).Should().Throw<ValidationException>().Where(ex => ex.Errors.ContainsKey("Title"))
  258. .And.Erros["Title"].Should().Contain("The specified title is already exists.");
  259. }
  260. [Test]
  261. public async Task ShouldUpdateTodoList()
  262. {
  263. var userId = await RunAsDefaultUserAsync();
  264. var listId = await SendAsync(new CreateTodoListCommand{
  265. Title = ""
  266. });
  267. var command = new UpdateTodoListCommand
  268. {
  269. Id = listId,
  270. Title= ""
  271. };
  272. await SendAsync(command);
  273. var list =await FindAsync<TodoList>(listId);
  274. list.Title.Should().Be(command.Title);
  275. list.LastModifiedBy.Should().NotBeNull();
  276. list.LastModifiedBy.Should().Be(userId);
  277. list.LastModified.Should().NotBeNull();
  278. list.LstModified.Should().BeCloaseTo(DateTime.Now, 1000);
  279. }
  280. }
  281. ```
  282. `Testing.cs`
  283. ```
  284. [SetUpFixture]
  285. public class Testing
  286. {
  287. private static IConfiguration _configuration;//配置
  288. private static IServiceScopeFactory _scopeFactory;//域
  289. private static Checkpoint _checkpoint;//Respawn
  290. private static string _currentUserId;
  291. [OneTimeSetUp]
  292. public void RunBeforeAnyTests()
  293. {
  294. var buidler = new ConfigurationBuilder()
  295. .SetBasePath(Directory.GetCurrentDirectory)
  296. .AddJsonFile("appsettings.json", true, true)
  297. .AddEnvironmentVariables();
  298. _configuration = builder.Build();
  299. var services = new ServiceCollection();//看作是容器
  300. var startup = new Startup(_configuration);//Startup.cs需要IConfiguration
  301. //ServiceCollection需要宿主
  302. services.AddSignleton(Mock.Of<IWebHostEnvironment>(w =>
  303. w.ApplcationName == "CleanTesting.WebUI" &&
  304. w.EnvirnmentName == "Development"));
  305. startup.ConfigureServices(services);
  306. //Replace service registration for ICurrentUserService
  307. //Remove existing registration
  308. var currentUserServiceDescriptor = services.FirstOrDefault(d => d.ServiceType == typeof(ICurrentUserService));
  309. service.Remove(currentUserServiceDescriptor);
  310. //Register testing version
  311. services.AddTransient(provider =>
  312. Mock.Of<ICurrentUserService>(s => s.UserId == _currentUserId));
  313. _scopeFactory = services.BuilderServiceProvider().GetService<IServiceScopeFactory>();
  314. _checkpoint = new Checkpoint{
  315. TablesToIgnore = new [] {"__EFMigrationsHistory"}
  316. };
  317. }
  318. public static async Task<string> RunAsDefaultUserAsync()
  319. {
  320. return await RundAsUserAsync("","");
  321. _currentUserId = null;
  322. }
  323. public static async Task<string> RunAsUserAsync(string userName, string password)
  324. {
  325. using var scope = _scopeFactory.CreateScope();
  326. var userManager = scope.ServiceProvider.GetService<UserManager<ApplicaitonUser>>();
  327. var user = new ApplicationUser{UserName=userName, Email = userName};
  328. var result = await userManager.CreateAsync(user, password);
  329. _currentUserId = user.Id;
  330. return _currentUserId;
  331. }
  332. public static async Task ResetState()
  333. {
  334. await _checkpoint.Reset(_configuration.GetConnectionString("DefaultConnection"));
  335. }
  336. public static async Task AddAsync<TEntity>(TEntity entity) where TEntity : class
  337. {
  338. using var scope = _scopeFactory.CreateScope();
  339. var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
  340. context.Add(entity);
  341. await context.SaveChangesAsync();
  342. }
  343. public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
  344. {
  345. using var scope = _scopeFacotry.CreateScope();
  346. var mediator = scope.ServiceProvider.GetService<IMediator>();
  347. return await mediator.Send(request);
  348. }
  349. public static async Task<TEntity> FindAsync<TEntity>(int id)
  350. {
  351. using var scope = _scopeFactory.CreateScope();
  352. var context = scope.ServiceProvider.GetService<ApplicationContext>();
  353. return await context.FindAsync<TEntity>(id);
  354. }
  355. }
  356. ```
  357. `appSetting.json`
  358. ```
  359. {
  360. "UseInMemoryDatabase":false,
  361. "ConnectionStrings":{
  362. "DefaultConnection" : "Server=(localdb)\\mssqllocaldb;Database=CleanTestingDb;Truested_Connection=True;MultipleActiveRestultSet"
  363. },
  364. "IdentityServer":{
  365. "Clients":{
  366. "CleanTesting.WebUI": {
  367. "Profile": "IdentityServerSPA"
  368. }
  369. },
  370. "Key":{
  371. "Type" : "Development"
  372. }
  373. }
  374. }
  375. ```
  376. `TestBase.cs`
  377. ```
  378. using static Testing;
  379. public calss TestBase
  380. {
  381. [SetUp]
  382. public async Task SetUp()
  383. {
  384. await ResetState();
  385. }
  386. }
  387. ```
  388. # 资源
  389. - `Clean Testing Sample Code`: `https://github.com/jasontaylordev/cleantesting`
  390. - `Rules to Better Unit Tests`: `https://rules.ssw.com.au/rules-to-better-unit-tests`(翻墙)
  391. - `Fixie Demo`: `https://github.com/fixie/fixie.demo`