From f7ec671a7422ea1fc6de4bb310fed3b4933f27e9 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 15 Jun 2020 00:05:02 +0800 Subject: refactor(back): Use generic host. --- Timeline.Tests/IntegratedTests/AuthorizationTest.cs | 6 ------ Timeline.Tests/IntegratedTests/FrontEndTest.cs | 5 ----- Timeline.Tests/IntegratedTests/IntegratedTestBase.cs | 18 ++++++++---------- Timeline.Tests/IntegratedTests/TimelineTest.cs | 6 ++---- Timeline.Tests/IntegratedTests/TokenTest.cs | 11 ++--------- Timeline.Tests/IntegratedTests/UnknownEndpointTest.cs | 5 ----- Timeline.Tests/IntegratedTests/UserAvatarTest.cs | 9 +-------- Timeline.Tests/IntegratedTests/UserTest.cs | 7 ------- 8 files changed, 13 insertions(+), 54 deletions(-) (limited to 'Timeline.Tests/IntegratedTests') diff --git a/Timeline.Tests/IntegratedTests/AuthorizationTest.cs b/Timeline.Tests/IntegratedTests/AuthorizationTest.cs index 4aa6b3ae..38071394 100644 --- a/Timeline.Tests/IntegratedTests/AuthorizationTest.cs +++ b/Timeline.Tests/IntegratedTests/AuthorizationTest.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; using System.Net; using System.Threading.Tasks; using Timeline.Tests.Helpers; @@ -9,11 +8,6 @@ namespace Timeline.Tests.IntegratedTests { public class AuthorizationTest : IntegratedTestBase { - public AuthorizationTest(WebApplicationFactory factory) - : base(factory) - { - } - private const string BaseUrl = "testing/auth/"; private const string AuthorizeUrl = BaseUrl + "Authorize"; private const string UserUrl = BaseUrl + "User"; diff --git a/Timeline.Tests/IntegratedTests/FrontEndTest.cs b/Timeline.Tests/IntegratedTests/FrontEndTest.cs index a00d41b1..39a6e545 100644 --- a/Timeline.Tests/IntegratedTests/FrontEndTest.cs +++ b/Timeline.Tests/IntegratedTests/FrontEndTest.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; using System.Net.Mime; using System.Threading.Tasks; using Timeline.Tests.Helpers; @@ -9,10 +8,6 @@ namespace Timeline.Tests.IntegratedTests { public class FrontEndTest : IntegratedTestBase { - public FrontEndTest(WebApplicationFactory factory) : base(factory) - { - } - [Fact] public async Task Index() { diff --git a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs index 01544828..b5aec512 100644 --- a/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs +++ b/Timeline.Tests/IntegratedTests/IntegratedTestBase.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; @@ -15,29 +15,27 @@ using Xunit; namespace Timeline.Tests.IntegratedTests { - public abstract class IntegratedTestBase : IClassFixture>, IAsyncLifetime + public abstract class IntegratedTestBase : 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) + public IntegratedTestBase() : this(1) { } - public IntegratedTestBase(WebApplicationFactory factory, int userCount) + public IntegratedTestBase(int userCount) { if (userCount < 0) throw new ArgumentOutOfRangeException(nameof(userCount), userCount, "User count can't be negative."); _userCount = userCount; - TestApp = new TestApplication(factory); + TestApp = new TestApplication(); } protected virtual Task OnInitializeAsync() @@ -59,7 +57,7 @@ namespace Timeline.Tests.IntegratedTests { await TestApp.InitializeAsync(); - using (var scope = Factory.Services.CreateScope()) + using (var scope = TestApp.Host.Services.CreateScope()) { var users = new List() { @@ -119,7 +117,7 @@ namespace Timeline.Tests.IntegratedTests public Task CreateDefaultClient(bool setApiBase = true) { - var client = Factory.CreateDefaultClient(); + var client = TestApp.Host.GetTestServer().CreateClient(); if (setApiBase) { client.BaseAddress = new Uri(client.BaseAddress, "api/"); @@ -129,7 +127,7 @@ namespace Timeline.Tests.IntegratedTests public async Task CreateClientWithCredential(string username, string password, bool setApiBase = true) { - var client = Factory.CreateDefaultClient(); + var client = TestApp.Host.GetTestServer().CreateClient(); if (setApiBase) { client.BaseAddress = new Uri(client.BaseAddress, "api/"); diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index d8017b8a..b6a56e94 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using SixLabors.ImageSharp; @@ -45,8 +44,7 @@ namespace Timeline.Tests.IntegratedTests public class TimelineTest : IntegratedTestBase { - public TimelineTest(WebApplicationFactory factory) - : base(factory, 3) + public TimelineTest() : base(3) { } @@ -1112,7 +1110,7 @@ namespace Timeline.Tests.IntegratedTests } { - using var scope = TestApp.Factory.Services.CreateScope(); + using var scope = TestApp.Host.Services.CreateScope(); var database = scope.ServiceProvider.GetRequiredService(); var count = await database.Data.CountAsync(); count.Should().Be(0); diff --git a/Timeline.Tests/IntegratedTests/TokenTest.cs b/Timeline.Tests/IntegratedTests/TokenTest.cs index 7b28746f..d1c31606 100644 --- a/Timeline.Tests/IntegratedTests/TokenTest.cs +++ b/Timeline.Tests/IntegratedTests/TokenTest.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; using System.Net.Http; @@ -17,12 +16,6 @@ namespace Timeline.Tests.IntegratedTests private const string CreateTokenUrl = "token/create"; private const string VerifyTokenUrl = "token/verify"; - public TokenTest(WebApplicationFactory factory) - : base(factory) - { - - } - private static async Task CreateUserTokenAsync(HttpClient client, string username, string password, int? expireOffset = null) { var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, Expire = expireOffset }); @@ -106,7 +99,7 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(); var token = (await CreateUserTokenAsync(client, "user1", "user1pw")).Token; - using (var scope = Factory.Services.CreateScope()) // UserService is scoped. + using (var scope = TestApp.Host.Services.CreateScope()) // UserService is scoped. { // create a user for test var userService = scope.ServiceProvider.GetRequiredService(); @@ -126,7 +119,7 @@ namespace Timeline.Tests.IntegratedTests using var client = await CreateDefaultClient(); var token = (await CreateUserTokenAsync(client, "user1", "user1pw")).Token; - using (var scope = Factory.Server.Host.Services.CreateScope()) // UserService is scoped. + using (var scope = TestApp.Host.Services.CreateScope()) // UserService is scoped. { var userService = scope.ServiceProvider.GetRequiredService(); await userService.DeleteUser("user1"); diff --git a/Timeline.Tests/IntegratedTests/UnknownEndpointTest.cs b/Timeline.Tests/IntegratedTests/UnknownEndpointTest.cs index 40f818a7..732232e2 100644 --- a/Timeline.Tests/IntegratedTests/UnknownEndpointTest.cs +++ b/Timeline.Tests/IntegratedTests/UnknownEndpointTest.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; using System.Threading.Tasks; using Timeline.Models.Http; using Timeline.Tests.Helpers; @@ -9,10 +8,6 @@ namespace Timeline.Tests.IntegratedTests { public class UnknownEndpointTest : IntegratedTestBase { - public UnknownEndpointTest(WebApplicationFactory factory) : base(factory) - { - } - [Fact] public async Task UnknownEndpoint() { diff --git a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs index fbf34490..91986cda 100644 --- a/Timeline.Tests/IntegratedTests/UserAvatarTest.cs +++ b/Timeline.Tests/IntegratedTests/UserAvatarTest.cs @@ -1,6 +1,5 @@ using FluentAssertions; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Gif; @@ -21,12 +20,6 @@ namespace Timeline.Tests.IntegratedTests { public class UserAvatarTest : IntegratedTestBase { - public UserAvatarTest(WebApplicationFactory factory) - : base(factory) - { - - } - [Fact] public async Task Test() { @@ -45,7 +38,7 @@ namespace Timeline.Tests.IntegratedTests .Which.Code.Should().Be(ErrorCodes.UserCommon.NotExist); } - var env = Factory.Server.Host.Services.GetRequiredService(); + var env = TestApp.Host.Services.GetRequiredService(); var defaultAvatarData = await File.ReadAllBytesAsync(Path.Combine(env.ContentRootPath, "default-avatar.png")); async Task GetReturnDefault(string username = "user1") diff --git a/Timeline.Tests/IntegratedTests/UserTest.cs b/Timeline.Tests/IntegratedTests/UserTest.cs index e226d084..7fd78d0e 100644 --- a/Timeline.Tests/IntegratedTests/UserTest.cs +++ b/Timeline.Tests/IntegratedTests/UserTest.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; using System.Collections.Generic; using System.Net; using System.Net.Http; @@ -12,12 +11,6 @@ namespace Timeline.Tests.IntegratedTests { public class UserTest : IntegratedTestBase { - public UserTest(WebApplicationFactory factory) - : base(factory) - { - - } - [Fact] public async Task GetList_NoAuth() { -- cgit v1.2.3