From 13f0a95397751783ac0b4dcfa4d8f1b7112de04c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 4 Aug 2019 14:47:17 +0800 Subject: Clean codes. --- Timeline.Tests/AuthorizationUnitTest.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'Timeline.Tests/AuthorizationUnitTest.cs') diff --git a/Timeline.Tests/AuthorizationUnitTest.cs b/Timeline.Tests/AuthorizationUnitTest.cs index ee3deac8..1f707f15 100644 --- a/Timeline.Tests/AuthorizationUnitTest.cs +++ b/Timeline.Tests/AuthorizationUnitTest.cs @@ -44,12 +44,11 @@ namespace Timeline.Tests [Fact] public async Task UserAuthorizationTest() { - using (var client = _factory.CreateDefaultClient()) + using (var client = await _factory.CreateClientWithUser("user", "user")) { - var token = (await client.CreateUserTokenAsync("user", "user")).Token; - var response1 = await client.SendWithAuthenticationAsync(token, UserUrl); + var response1 = await client.GetAsync(UserUrl); Assert.Equal(HttpStatusCode.OK, response1.StatusCode); - var response2 = await client.SendWithAuthenticationAsync(token, AdminUrl); + var response2 = await client.GetAsync(AdminUrl); Assert.Equal(HttpStatusCode.Forbidden, response2.StatusCode); } } -- cgit v1.2.3 From 2a32e03a384a30b14988b0b6e40db845f4a5444e Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 4 Aug 2019 21:30:53 +0800 Subject: Fix the database bug in unit tests. --- Timeline.Tests/AuthorizationUnitTest.cs | 6 +- Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 83 ++++++++++++++++++++++ .../Helpers/WebApplicationFactoryExtensions.cs | 59 --------------- Timeline.Tests/TokenUnitTest.cs | 8 +-- Timeline.Tests/UserUnitTest.cs | 6 +- 5 files changed, 92 insertions(+), 70 deletions(-) create mode 100644 Timeline.Tests/Helpers/MyWebApplicationFactory.cs delete mode 100644 Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs (limited to 'Timeline.Tests/AuthorizationUnitTest.cs') diff --git a/Timeline.Tests/AuthorizationUnitTest.cs b/Timeline.Tests/AuthorizationUnitTest.cs index 1f707f15..a25a8f9b 100644 --- a/Timeline.Tests/AuthorizationUnitTest.cs +++ b/Timeline.Tests/AuthorizationUnitTest.cs @@ -8,7 +8,7 @@ using Xunit.Abstractions; namespace Timeline.Tests { - public class AuthorizationUnitTest : IClassFixture> + public class AuthorizationUnitTest : IClassFixture> { private const string AuthorizeUrl = "Test/User/Authorize"; private const string UserUrl = "Test/User/User"; @@ -16,9 +16,9 @@ namespace Timeline.Tests private readonly WebApplicationFactory _factory; - public AuthorizationUnitTest(WebApplicationFactory factory, ITestOutputHelper outputHelper) + public AuthorizationUnitTest(MyWebApplicationFactory factory, ITestOutputHelper outputHelper) { - _factory = factory.WithTestConfig(outputHelper); + _factory = factory.WithTestLogging(outputHelper); } [Fact] diff --git a/Timeline.Tests/Helpers/MyWebApplicationFactory.cs b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs new file mode 100644 index 00000000..903cd670 --- /dev/null +++ b/Timeline.Tests/Helpers/MyWebApplicationFactory.cs @@ -0,0 +1,83 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Data.Sqlite; +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 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() + { + _databaseConnection = new SqliteConnection("Data Source=:memory:;"); + _databaseConnection.Open(); + + InitDatabase(); + } + + private void InitDatabase() + { + var options = new DbContextOptionsBuilder() + .UseSqlite(_databaseConnection) + .Options; + + using (var context = new DatabaseContext(options)) + { + context.Database.EnsureCreated(); + context.Users.AddRange(TestMockUsers.MockUsers); + context.SaveChanges(); + } + } + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.ConfigureServices(services => + { + services.AddEntityFrameworkSqlite(); + services.AddDbContext(options => + { + options.UseSqlite(_databaseConnection); + }); + }) + .ConfigureTestServices(services => + { + services.AddSingleton(); + }); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _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 => + { + logging.AddXunit(outputHelper); + }); + }); + } + } +} 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 WithTestConfig(this WebApplicationFactory 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(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(); - - // Ensure the database is created. - db.Database.EnsureCreated(); - - db.Users.AddRange(TestMockUsers.MockUsers); - db.SaveChanges(); - } - }) - .ConfigureTestServices(services => - { - services.AddSingleton(); - }); - }); - } - } -} diff --git a/Timeline.Tests/TokenUnitTest.cs b/Timeline.Tests/TokenUnitTest.cs index 1fe3cff6..7b83cd13 100644 --- a/Timeline.Tests/TokenUnitTest.cs +++ b/Timeline.Tests/TokenUnitTest.cs @@ -6,9 +6,7 @@ using System.Linq; using System.Net; using System.Net.Http; using Timeline.Controllers; -using Timeline.Entities; using Timeline.Entities.Http; -using Timeline.Models; using Timeline.Services; using Timeline.Tests.Helpers; using Timeline.Tests.Helpers.Authentication; @@ -17,16 +15,16 @@ using Xunit.Abstractions; namespace Timeline.Tests { - public class TokenUnitTest : IClassFixture> + public class TokenUnitTest : IClassFixture> { private const string CreateTokenUrl = "token/create"; private const string VerifyTokenUrl = "token/verify"; private readonly WebApplicationFactory _factory; - public TokenUnitTest(WebApplicationFactory factory, ITestOutputHelper outputHelper) + public TokenUnitTest(MyWebApplicationFactory factory, ITestOutputHelper outputHelper) { - _factory = factory.WithTestConfig(outputHelper); + _factory = factory.WithTestLogging(outputHelper); } [Fact] diff --git a/Timeline.Tests/UserUnitTest.cs b/Timeline.Tests/UserUnitTest.cs index a4b4dace..b3377f7b 100644 --- a/Timeline.Tests/UserUnitTest.cs +++ b/Timeline.Tests/UserUnitTest.cs @@ -11,13 +11,13 @@ using Xunit.Abstractions; namespace Timeline.Tests { - public class UserUnitTest : IClassFixture> + public class UserUnitTest : IClassFixture> { private readonly WebApplicationFactory _factory; - public UserUnitTest(WebApplicationFactory factory, ITestOutputHelper outputHelper) + public UserUnitTest(MyWebApplicationFactory factory, ITestOutputHelper outputHelper) { - _factory = factory.WithTestConfig(outputHelper); + _factory = factory.WithTestLogging(outputHelper); } [Fact] -- cgit v1.2.3