From 5a90de954ee2d975cd038438495dfaa5a8d9d6d7 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 11 Mar 2020 23:11:58 +0800 Subject: Make all integrated tests async. --- .../IntegratedTests/IntegratedTestBase.cs | 61 +++++++++++++++------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs') diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs index 66904629..a4a7638c 100644 --- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs +++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -15,12 +15,16 @@ using Xunit; namespace Timeline.Tests.IntegratedTests { - public abstract class IntegratedTestBase : IClassFixture>, IDisposable + public abstract class IntegratedTestBase : IClassFixture>, IAsyncLifetime { protected TestApplication TestApp { get; } protected WebApplicationFactory Factory => TestApp.Factory; + public IReadOnlyList UserInfos { get; private set; } + + private readonly int _userCount; + public IntegratedTestBase(WebApplicationFactory factory) : this(factory, 1) { @@ -31,8 +35,30 @@ namespace Timeline.Tests.IntegratedTests if (userCount < 0) throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative."); - TestApp = new TestApplication(factory); + _userCount = userCount; + TestApp = new TestApplication(factory); + } + + protected virtual Task OnInitializeAsync() + { + return Task.CompletedTask; + } + + protected virtual Task OnDisposeAsync() + { + return Task.CompletedTask; + } + + protected virtual void OnDispose() + { + + } + + public async Task InitializeAsync() + { + await TestApp.InitializeAsync(); + using (var scope = Factory.Services.CreateScope()) { var users = new List() @@ -46,7 +72,7 @@ namespace Timeline.Tests.IntegratedTests } }; - for (int i = 1; i <= userCount; i++) + for (int i = 1; i <= _userCount; i++) { users.Add(new User { @@ -62,10 +88,10 @@ namespace Timeline.Tests.IntegratedTests var userService = scope.ServiceProvider.GetRequiredService(); foreach (var user in users) { - userService.CreateUser(user).Wait(); + await userService.CreateUser(user); } - using var client = CreateDefaultClient().Result; + using var client = await CreateDefaultClient(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase @@ -74,26 +100,23 @@ namespace Timeline.Tests.IntegratedTests options.Converters.Add(new JsonDateTimeConverter()); foreach (var user in users) { - var s = client.GetStringAsync($"/users/{user.Username}").Result; + var s = await client.GetStringAsync($"/users/{user.Username}"); userInfoList.Add(JsonSerializer.Deserialize(s, options)); } UserInfos = userInfoList; } + + await OnInitializeAsync(); + } + + public async Task DisposeAsync() + { + await OnDisposeAsync(); + OnDispose(); + await TestApp.DisposeAsync(); } - protected virtual void OnDispose() - { - } - - public void Dispose() - { - OnDispose(); - TestApp.Dispose(); - } - - public IReadOnlyList UserInfos { get; } - public Task CreateDefaultClient() { return Task.FromResult(Factory.CreateDefaultClient()); @@ -128,6 +151,6 @@ namespace Timeline.Tests.IntegratedTests public Task CreateClientAsUser() { return CreateClientAs(1); - } + } } } -- cgit v1.2.3