From fa2a3282c51d831b25f374803301e75eac15d11c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 17 Oct 2019 20:46:57 +0800 Subject: ... --- Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 73 ----------------------- 1 file changed, 73 deletions(-) delete mode 100644 Timeline.Tests/Helpers/MyWebApplicationFactory.cs (limited to 'Timeline.Tests/Helpers/MyWebApplicationFactory.cs') diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs deleted file mode 100644 index dfbe6620..00000000 --- a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs +++ /dev/null @@ -1,73 +0,0 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.AspNetCore.TestHost; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Diagnostics; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using System; -using Timeline.Entities; -using Timeline.Services; -using Timeline.Tests.Mock.Data; -using Timeline.Tests.Mock.Services; -using Xunit.Abstractions; - -namespace Timeline.Tests.Helpers -{ - public class MyWebApplicationFactory : WebApplicationFactory where TStartup : class - { - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - builder.ConfigureTestServices(services => - { - services.AddSingleton(); - }); - } - } - - public static class WebApplicationFactoryExtensions - { - public static WebApplicationFactory WithTestConfig(this WebApplicationFactory factory, ITestOutputHelper outputHelper, out Action disposeAction) where TEntry : 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 . - SqliteConnection _databaseConnection = new SqliteConnection("Data Source=:memory:;"); - _databaseConnection.Open(); - - { - var options = new DbContextOptionsBuilder() - .UseSqlite(_databaseConnection) - .ConfigureWarnings(builder => - { - builder.Throw(RelationalEventId.QueryClientEvaluationWarning); - }) - .Options; - - using (var context = new DatabaseContext(options)) - { - TestDatabase.InitDatabase(context); - }; - } - - disposeAction = () => - { - _databaseConnection.Close(); - _databaseConnection.Dispose(); - }; - - return factory.WithWebHostBuilder(builder => - { - builder.ConfigureTestLogging() - .ConfigureServices(services => - { - services.AddEntityFrameworkSqlite(); - services.AddDbContext(options => - { - options.UseSqlite(_databaseConnection); - }); - }); - }); - } - } -} -- cgit v1.2.3