From 4791d81ffe50d20c5e7e2f6f2ef91b519cb639ea Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sat, 17 Aug 2019 20:33:01 +0800 Subject: Finally solve the database conflict problem in unit tests. --- Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 81 ++++++++++------------- 1 file changed, 35 insertions(+), 46 deletions(-) (limited to 'Timeline.Tests/Helpers/MyWebApplicationFactory.cs') 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 : WebApplicationFactory 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(); + }); } + } - private void InitDatabase() + public static class WebApplicationFactoryExtensions + { + public static WebApplicationFactory WithTestConfig(this WebApplicationFactory factory, ITestOutputHelper outputHelper, out Action disposeAction) where TEntry : class { - var options = new DbContextOptionsBuilder() - .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() + .UseSqlite(_databaseConnection) + .Options; - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - builder.ConfigureServices(services => - { - services.AddEntityFrameworkSqlite(); - services.AddDbContext(options => + using (var context = new DatabaseContext(options)) { - options.UseSqlite(_databaseConnection); - }); - }) - .ConfigureTestServices(services => - { - services.AddSingleton(); - }); - } + 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 WithTestLogging(this WebApplicationFactory 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(options => + { + options.UseSqlite(_databaseConnection); + }); }); }); } -- cgit v1.2.3