aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-03-12 19:56:20 +0800
committerGitHub <noreply@github.com>2020-03-12 19:56:20 +0800
commit904f98bda60b3bd92331aacde3771dedde62d2b5 (patch)
tree70681348ddfc3bc8c3d9a92ae010a02020830573 /Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
parenta37874830399c193392cc78367efcecbe8275ceb (diff)
parentf8ff7e20eb5d5673575d36b8964a013765b77bf8 (diff)
downloadtimeline-904f98bda60b3bd92331aacde3771dedde62d2b5.tar.gz
timeline-904f98bda60b3bd92331aacde3771dedde62d2b5.tar.bz2
timeline-904f98bda60b3bd92331aacde3771dedde62d2b5.zip
Merge pull request #69 from crupest/image
Post image feature.
Diffstat (limited to 'Timeline.Tests/IntegratedTests/IntegratedTestBase.cs')
-rw-r--r--Timeline.Tests/IntegratedTests/IntegratedTestBase.cs84
1 files changed, 57 insertions, 27 deletions
diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
index dfde2ea5..a4a7638c 100644
--- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
+++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs
@@ -1,10 +1,13 @@
-using AutoMapper;
-using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
+using System.Text.Json;
+using System.Text.Json.Serialization;
using System.Threading.Tasks;
+using Timeline.Models;
+using Timeline.Models.Converters;
using Timeline.Models.Http;
using Timeline.Services;
using Timeline.Tests.Helpers;
@@ -12,18 +15,16 @@ using Xunit;
namespace Timeline.Tests.IntegratedTests
{
- public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IDisposable
+ public abstract class IntegratedTestBase : IClassFixture<WebApplicationFactory<Startup>>, IAsyncLifetime
{
- static IntegratedTestBase()
- {
- FluentAssertions.AssertionOptions.AssertEquivalencyUsing(options =>
- options.Excluding(m => m.RuntimeType == typeof(UserInfoLinks)));
- }
-
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)
{
@@ -34,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>()
@@ -49,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
{
@@ -63,30 +86,37 @@ namespace Timeline.Tests.IntegratedTests
var userInfoList = new List<UserInfo>();
var userService = scope.ServiceProvider.GetRequiredService<IUserService>();
- var mapper = scope.ServiceProvider.GetRequiredService<IMapper>();
+ foreach (var user in users)
+ {
+ await userService.CreateUser(user);
+ }
+ using var client = await CreateDefaultClient();
+ var options = new JsonSerializerOptions
+ {
+ PropertyNamingPolicy = JsonNamingPolicy.CamelCase
+ };
+ options.Converters.Add(new JsonStringEnumConverter());
+ options.Converters.Add(new JsonDateTimeConverter());
foreach (var user in users)
{
- userService.CreateUser(user).Wait();
- userInfoList.Add(mapper.Map<UserInfo>(user));
+ 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());
@@ -121,6 +151,6 @@ namespace Timeline.Tests.IntegratedTests
public Task<HttpClient> CreateClientAsUser()
{
return CreateClientAs(1);
- }
+ }
}
}