diff options
author | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-10-27 19:21:35 +0800 |
commit | 05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33 (patch) | |
tree | 929e514de85eb82a5acb96ecffc6e6d2d95f878f /BackEnd/Timeline.Tests/Helpers/TestApplication.cs | |
parent | 986c6f2e3b858d6332eba0b42acc6861cd4d0227 (diff) | |
download | timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.tar.gz timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.tar.bz2 timeline-05ccb4d8f1bbe3fb64e117136b4a89bcfb0b0b33.zip |
Split front and back end.
Diffstat (limited to 'BackEnd/Timeline.Tests/Helpers/TestApplication.cs')
-rw-r--r-- | BackEnd/Timeline.Tests/Helpers/TestApplication.cs | 72 |
1 files changed, 72 insertions, 0 deletions
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<string, string>
+ {
+ [ApplicationConfiguration.UseMockFrontEndKey] = "true",
+ ["WorkDir"] = WorkDir
+ });
+ })
+ .ConfigureServices(services =>
+ {
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseSqlite(Database.Connection);
+ });
+ })
+ .ConfigureWebHost(webBuilder =>
+ {
+ webBuilder
+ .UseTestServer()
+ .UseStartup<Startup>();
+ })
+ .StartAsync();
+ }
+
+ public async Task DisposeAsync()
+ {
+ await Host.StopAsync();
+ Host.Dispose();
+
+ Directory.Delete(WorkDir, true);
+
+ await Database.DisposeAsync();
+ }
+ }
+}
|