aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-04 21:30:53 +0800
committer杨宇千 <crupest@outlook.com>2019-08-04 21:30:53 +0800
commitd1ebb882e8484c80eca86cac80602fb4c1401834 (patch)
treed1c9c7b51353b67b47bb4cd89aa82754ef0a1234 /Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
parent1f56e4857892150e1109a93fadc487f92d955eed (diff)
downloadtimeline-d1ebb882e8484c80eca86cac80602fb4c1401834.tar.gz
timeline-d1ebb882e8484c80eca86cac80602fb4c1401834.tar.bz2
timeline-d1ebb882e8484c80eca86cac80602fb4c1401834.zip
Fix the database bug in unit tests.
Diffstat (limited to 'Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs')
-rw-r--r--Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs59
1 files changed, 0 insertions, 59 deletions
diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
deleted file mode 100644
index 5a1f97d5..00000000
--- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Mvc.Testing;
-using Microsoft.AspNetCore.TestHost;
-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 static class WebApplicationFactoryExtensions
- {
- public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper) where TEntry : class
- {
- return factory.WithWebHostBuilder(builder =>
- {
- builder
- .ConfigureLogging(logging =>
- {
- logging.AddXunit(outputHelper);
- })
- .ConfigureServices(services =>
- {
- var serviceProvider = new ServiceCollection()
- .AddEntityFrameworkSqlite()
- .BuildServiceProvider();
-
- services.AddDbContext<DatabaseContext>(options =>
- {
- options.UseSqlite("Data Source=:memory:;"); //TODO! This not work!
- options.UseInternalServiceProvider(serviceProvider);
- });
-
- var sp = services.BuildServiceProvider();
-
- // Create a scope to obtain a reference to the database
- // context (ApplicationDbContext).
- using (var scope = sp.CreateScope())
- {
- var scopedServices = scope.ServiceProvider;
- var db = scopedServices.GetRequiredService<DatabaseContext>();
-
- // Ensure the database is created.
- db.Database.EnsureCreated();
-
- db.Users.AddRange(TestMockUsers.MockUsers);
- db.SaveChanges();
- }
- })
- .ConfigureTestServices(services =>
- {
- services.AddSingleton<IClock, TestClock>();
- });
- });
- }
- }
-}