diff options
Diffstat (limited to 'Timeline.Tests/Helpers/MyWebApplicationFactory.cs')
-rw-r--r-- | Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 81 |
1 files changed, 35 insertions, 46 deletions
diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs index b49756e4..dfadd1ae 100644 --- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs +++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs @@ -5,6 +5,7 @@ using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
+using System;
using Timeline.Entities;
using Timeline.Services;
using Timeline.Tests.Mock.Data;
@@ -15,69 +16,57 @@ 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()
+ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
- _databaseConnection = new SqliteConnection("Data Source=:memory:;");
- _databaseConnection.Open();
-
- InitDatabase();
+ builder.ConfigureTestServices(services =>
+ {
+ services.AddSingleton<IClock, TestClock>();
+ });
}
+ }
- private void InitDatabase()
+ public static class WebApplicationFactoryExtensions
+ {
+ public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper, out Action disposeAction) where TEntry : class
{
- var options = new DbContextOptionsBuilder<DatabaseContext>()
- .UseSqlite(_databaseConnection)
- .Options;
+ // 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 .
+ SqliteConnection _databaseConnection = new SqliteConnection("Data Source=:memory:;");
+ _databaseConnection.Open();
- using (var context = new DatabaseContext(options))
{
- context.Database.EnsureCreated();
- context.Users.AddRange(MockUsers.Users);
- context.SaveChanges();
- }
- }
+ var options = new DbContextOptionsBuilder<DatabaseContext>()
+ .UseSqlite(_databaseConnection)
+ .Options;
- protected override void ConfigureWebHost(IWebHostBuilder builder)
- {
- builder.ConfigureServices(services =>
- {
- services.AddEntityFrameworkSqlite();
- services.AddDbContext<DatabaseContext>(options =>
+ using (var context = new DatabaseContext(options))
{
- options.UseSqlite(_databaseConnection);
- });
- })
- .ConfigureTestServices(services =>
- {
- services.AddSingleton<IClock, TestClock>();
- });
- }
+ context.Database.EnsureCreated();
+ context.Users.AddRange(MockUsers.Users);
+ context.SaveChanges();
+ };
+ }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
+ disposeAction = () =>
{
_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 =>
+ builder
+ .ConfigureLogging(logging =>
{
logging.AddXunit(outputHelper);
+ })
+ .ConfigureServices(services =>
+ {
+ services.AddEntityFrameworkSqlite();
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseSqlite(_databaseConnection);
+ });
});
});
}
|