From e0b4c538228864c314900affae08df72edc9cd60 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/Helpers/TestApplication.cs | 35 +++++++++++++++++++------------ 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'Timeline.Tests/Helpers/TestApplication.cs') diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs index abdb0a60..45807516 100644 --- a/Timeline.Tests/Helpers/TestApplication.cs +++ b/Timeline.Tests/Helpers/TestApplication.cs @@ -1,8 +1,10 @@ -using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.TestHost; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -17,13 +19,13 @@ namespace Timeline.Tests.Helpers { public SqliteConnection DatabaseConnection { get; private set; } - public WebApplicationFactory Factory { get; private set; } + public IHost Host { get; private set; } public string WorkDir { get; private set; } - public TestApplication(WebApplicationFactory factory) + public TestApplication() { - Factory = factory; + } public async Task InitializeAsync() @@ -35,8 +37,7 @@ namespace Timeline.Tests.Helpers await DatabaseConnection.OpenAsync(); var options = new DbContextOptionsBuilder() - .UseSqlite(DatabaseConnection) - .Options; + .UseSqlite(DatabaseConnection).Options; using (var context = new DatabaseContext(options)) { @@ -48,28 +49,36 @@ namespace Timeline.Tests.Helpers await context.SaveChangesAsync(); } - Factory = Factory.WithWebHostBuilder(builder => - { - builder.ConfigureAppConfiguration((context, config) => + Host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder() + .ConfigureAppConfiguration((context, config) => { config.AddInMemoryCollection(new Dictionary { [ApplicationConfiguration.UseMockFrontEndKey] = "true", ["WorkDir"] = WorkDir }); - }); - builder.ConfigureServices(services => + }) + .ConfigureServices(services => { services.AddDbContext(options => { options.UseSqlite(DatabaseConnection); }); - }); - }); + }) + .ConfigureWebHost(webBuilder => + { + webBuilder + .UseTestServer() + .UseStartup(); + }) + .StartAsync(); } public async Task DisposeAsync() { + await Host.StopAsync(); + Host.Dispose(); + await DatabaseConnection.CloseAsync(); await DatabaseConnection.DisposeAsync(); Directory.Delete(WorkDir, true); -- cgit v1.2.3