From ac769e656b122ff569c3f1534701b71e00fed586 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 19:21:35 +0800 Subject: Split front and back end. --- BackEnd/Timeline.Tests/Helpers/TestApplication.cs | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 BackEnd/Timeline.Tests/Helpers/TestApplication.cs (limited to 'BackEnd/Timeline.Tests/Helpers/TestApplication.cs') diff --git a/BackEnd/Timeline.Tests/Helpers/TestApplication.cs b/BackEnd/Timeline.Tests/Helpers/TestApplication.cs new file mode 100644 index 00000000..684ffe2c --- /dev/null +++ b/BackEnd/Timeline.Tests/Helpers/TestApplication.cs @@ -0,0 +1,72 @@ +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; +using Timeline.Configs; +using Timeline.Entities; +using Xunit; + +namespace Timeline.Tests.Helpers +{ + public class TestApplication : IAsyncLifetime + { + public TestDatabase Database { get; } + + public IHost Host { get; private set; } + + public string WorkDir { get; private set; } + + public TestApplication() + { + Database = new TestDatabase(false); + } + + public async Task InitializeAsync() + { + await Database.InitializeAsync(); + + WorkDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + Directory.CreateDirectory(WorkDir); + + Host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder() + .ConfigureAppConfiguration((context, config) => + { + config.AddInMemoryCollection(new Dictionary + { + [ApplicationConfiguration.UseMockFrontEndKey] = "true", + ["WorkDir"] = WorkDir + }); + }) + .ConfigureServices(services => + { + services.AddDbContext(options => + { + options.UseSqlite(Database.Connection); + }); + }) + .ConfigureWebHost(webBuilder => + { + webBuilder + .UseTestServer() + .UseStartup(); + }) + .StartAsync(); + } + + public async Task DisposeAsync() + { + await Host.StopAsync(); + Host.Dispose(); + + Directory.Delete(WorkDir, true); + + await Database.DisposeAsync(); + } + } +} -- cgit v1.2.3