aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-04 21:35:04 +0800
committerGitHub <noreply@github.com>2019-08-04 21:35:04 +0800
commit52f5d2724d74ddb37141d8dfcc8384c0c8ce0e24 (patch)
treed1c9c7b51353b67b47bb4cd89aa82754ef0a1234 /Timeline.Tests/Helpers/MyWebApplicationFactory.cs
parentc07297373df08bd605f5bec96020192bb6dec151 (diff)
parentd1ebb882e8484c80eca86cac80602fb4c1401834 (diff)
downloadtimeline-52f5d2724d74ddb37141d8dfcc8384c0c8ce0e24.tar.gz
timeline-52f5d2724d74ddb37141d8dfcc8384c0c8ce0e24.tar.bz2
timeline-52f5d2724d74ddb37141d8dfcc8384c0c8ce0e24.zip
Merge pull request #34 from crupest/token-time
Set token expired time and write unit tests.
Diffstat (limited to 'Timeline.Tests/Helpers/MyWebApplicationFactory.cs')
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs83
1 files changed, 83 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
new file mode 100644
index 00000000..903cd670
--- /dev/null
+++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs
@@ -0,0 +1,83 @@
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.AspNetCore.TestHost;
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using Timeline.Models;
+using Timeline.Services;
+using Xunit.Abstractions;
+
+namespace Timeline.Tests.Helpers
+{
+ public class MyWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
+ {
+ // We should keep the connection, so the database is persisted but not recreate every time.
+ // See https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite#writing-tests .
+ private readonly SqliteConnection _databaseConnection;
+
+ public MyWebApplicationFactory() : base()
+ {
+ _databaseConnection = new SqliteConnection("Data Source=:memory:;");
+ _databaseConnection.Open();
+
+ InitDatabase();
+ }
+
+ private void InitDatabase()
+ {
+ var options = new DbContextOptionsBuilder<DatabaseContext>()
+ .UseSqlite(_databaseConnection)
+ .Options;
+
+ using (var context = new DatabaseContext(options))
+ {
+ context.Database.EnsureCreated();
+ context.Users.AddRange(TestMockUsers.MockUsers);
+ context.SaveChanges();
+ }
+ }
+
+ protected override void ConfigureWebHost(IWebHostBuilder builder)
+ {
+ builder.ConfigureServices(services =>
+ {
+ services.AddEntityFrameworkSqlite();
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseSqlite(_databaseConnection);
+ });
+ })
+ .ConfigureTestServices(services =>
+ {
+ services.AddSingleton<IClock, TestClock>();
+ });
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _databaseConnection.Close();
+ _databaseConnection.Dispose();
+ }
+
+ base.Dispose(disposing);
+ }
+ }
+
+ public static class WebApplicationFactoryExtensions
+ {
+ public static WebApplicationFactory<TEntry> WithTestLogging<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper) where TEntry : class
+ {
+ return factory.WithWebHostBuilder(builder =>
+ {
+ builder.ConfigureLogging(logging =>
+ {
+ logging.AddXunit(outputHelper);
+ });
+ });
+ }
+ }
+}