diff options
Diffstat (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r-- | Timeline.Tests/IntegratedTests/IntegratedTestBase.cs | 61 |
1 files changed, 42 insertions, 19 deletions
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<WebApplicationFactory<Startup>>, IDisposable + public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IAsyncLifetime { protected TestApplication TestApp { get; } protected WebApplicationFactory<Startup> Factory => TestApp.Factory; + public IReadOnlyList<UserInfo> UserInfos { get; private set; } + + private readonly int _userCount; + public IntegratedTestBase(WebApplicationFactory<Startup> 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<User>()
@@ -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<IUserService>();
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<UserInfo>(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<UserInfo> UserInfos { get; } - public Task<HttpClient> CreateDefaultClient() { return Task.FromResult(Factory.CreateDefaultClient()); @@ -128,6 +151,6 @@ namespace Timeline.Tests.IntegratedTests public Task<HttpClient> CreateClientAsUser()
{
return CreateClientAs(1);
- } + }
} } |