diff options
author | 杨宇千 <crupest@outlook.com> | 2019-04-13 15:47:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-13 15:47:40 +0800 |
commit | 56c51bdf844ce1e3642dcdc4099187e7e57008c7 (patch) | |
tree | 1aa37565aad734b604eb94ed3a62db2308f4b30d /Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs | |
parent | 19cae15eba2bcede41b818e1b8ab7fd5ac92eb05 (diff) | |
parent | 108ea333534445a8c76d4db632ebf21abf426c71 (diff) | |
download | timeline-56c51bdf844ce1e3642dcdc4099187e7e57008c7.tar.gz timeline-56c51bdf844ce1e3642dcdc4099187e7e57008c7.tar.bz2 timeline-56c51bdf844ce1e3642dcdc4099187e7e57008c7.zip |
Merge pull request #20 from crupest/separate
Separate front end and back end.
Diffstat (limited to 'Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs')
-rw-r--r-- | Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs index bb8fc71b..4a7f87fb 100644 --- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs +++ b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs @@ -1,6 +1,10 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Timeline.Models; +using Timeline.Services; using Xunit.Abstractions; namespace Timeline.Tests.Helpers @@ -16,6 +20,52 @@ namespace Timeline.Tests.Helpers .ConfigureLogging(logging => { logging.AddXunit(outputHelper); + }) + .ConfigureServices(services => + { + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkInMemoryDatabase() + .BuildServiceProvider(); + + services.AddDbContext<DatabaseContext>(options => + { + options.UseInMemoryDatabase("timeline"); + 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>(); + + var passwordService = new PasswordService(null); + + // Ensure the database is created. + db.Database.EnsureCreated(); + + db.Users.AddRange(new User[] { + new User + { + Id = 0, + Name = "user", + EncryptedPassword = passwordService.HashPassword("user"), + RoleString = "user" + }, + new User + { + Id = 0, + Name = "admin", + EncryptedPassword = passwordService.HashPassword("admin"), + RoleString = "user,admin" + } + }); + + db.SaveChanges(); + } }); }); } |