From def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 17 Oct 2019 20:46:57 +0800 Subject: ... --- .../Helpers/AssertionResponseExtensions.cs | 24 ++++--- .../Authentication/AuthenticationExtensions.cs | 6 +- Timeline.Tests/Helpers/InvalidModelTestHelpers.cs | 5 +- Timeline.Tests/Helpers/MyTestLoggerFactory.cs | 25 -------- Timeline.Tests/Helpers/MyWebApplicationFactory.cs | 73 ---------------------- Timeline.Tests/Helpers/TestApplication.cs | 52 +++++++++++++++ 6 files changed, 74 insertions(+), 111 deletions(-) delete mode 100644 Timeline.Tests/Helpers/MyTestLoggerFactory.cs delete mode 100644 Timeline.Tests/Helpers/MyWebApplicationFactory.cs create mode 100644 Timeline.Tests/Helpers/TestApplication.cs (limited to 'Timeline.Tests/Helpers') diff --git a/Timeline.Tests/Helpers/AssertionResponseExtensions.cs b/Timeline.Tests/Helpers/AssertionResponseExtensions.cs index e67a172a..c7ebdb7a 100644 --- a/Timeline.Tests/Helpers/AssertionResponseExtensions.cs +++ b/Timeline.Tests/Helpers/AssertionResponseExtensions.cs @@ -110,29 +110,39 @@ namespace Timeline.Tests.Helpers return assertions.HaveBodyAsJson(because, becauseArgs); } + public static AndWhichConstraint> HaveBodyAsCommonDataResponse(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) + { + return assertions.HaveBodyAsJson>(because, becauseArgs); + } + public static void HaveBodyAsCommonResponseWithCode(this HttpResponseMessageAssertions assertions, int expected, string because = "", params object[] becauseArgs) { assertions.HaveBodyAsCommonResponse(because, becauseArgs).Which.Code.Should().Be(expected, because, becauseArgs); } - public static void BePutCreated(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) + public static void HaveBodyAsCommonDataResponseWithCode(this HttpResponseMessageAssertions assertions, int expected, string because = "", params object[] becauseArgs) + { + assertions.HaveBodyAsCommonDataResponse(because, becauseArgs).Which.Code.Should().Be(expected, because, becauseArgs); + } + + public static void BePutCreate(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) { - assertions.HaveStatusCodeCreated(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonPutResponse.Created, because, becauseArgs); + assertions.HaveStatusCodeCreated(because, becauseArgs).And.Should().HaveBodyAsCommonDataResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonPutResponse.Create(), because, becauseArgs); } - public static void BePutModified(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) + public static void BePutModify(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) { - assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonPutResponse.Modified, because, becauseArgs); + assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonDataResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonPutResponse.Modify(), because, becauseArgs); } - public static void BeDeleteDeleted(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) + public static void BeDeleteDelete(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) { - assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonDeleteResponse.Deleted, because, becauseArgs); + assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonDataResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonDeleteResponse.Delete(), because, becauseArgs); } public static void BeDeleteNotExist(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs) { - assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonDeleteResponse.NotExists, because, becauseArgs); + assertions.HaveStatusCodeOk(because, becauseArgs).And.Should().HaveBodyAsCommonDataResponse(because, becauseArgs).Which.Should().BeEquivalentTo(CommonDeleteResponse.NotExist(), because, becauseArgs); } } } diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs index c8a79e58..d068a08a 100644 --- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs +++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs @@ -13,7 +13,7 @@ namespace Timeline.Tests.Helpers.Authentication public static async Task CreateUserTokenAsync(this HttpClient client, string username, string password, int? expireOffset = null) { - var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, ExpireOffset = expireOffset }); + var response = await client.PostAsJsonAsync(CreateTokenUrl, new CreateTokenRequest { Username = username, Password = password, Expire = expireOffset }); response.Should().HaveStatusCodeOk(); var result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return result; @@ -29,12 +29,12 @@ namespace Timeline.Tests.Helpers.Authentication public static Task CreateClientAsUser(this WebApplicationFactory factory) where T : class { - return factory.CreateClientWithCredential(MockUsers.UserUsername, MockUsers.UserPassword); + return factory.CreateClientWithCredential(MockUser.User.Username, MockUser.User.Password); } public static Task CreateClientAsAdmin(this WebApplicationFactory factory) where T : class { - return factory.CreateClientWithCredential(MockUsers.AdminUsername, MockUsers.AdminPassword); + return factory.CreateClientWithCredential(MockUser.Admin.Username, MockUser.Admin.Password); } } } diff --git a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs index af432095..4a445ca4 100644 --- a/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs +++ b/Timeline.Tests/Helpers/InvalidModelTestHelpers.cs @@ -1,6 +1,5 @@ using System.Net.Http; using System.Threading.Tasks; -using Timeline.Models.Http; namespace Timeline.Tests.Helpers { @@ -10,14 +9,14 @@ namespace Timeline.Tests.Helpers { var response = await client.PostAsJsonAsync(url, body); response.Should().HaveStatusCodeBadRequest() - .And.Should().HaveBodyAsCommonResponseWithCode(CommonResponse.ErrorCodes.InvalidModel); + .And.Should().HaveBodyAsCommonResponseWithCode(ErrorCodes.Http.Common.InvalidModel); } public static async Task TestPutInvalidModel(HttpClient client, string url, T body) { var response = await client.PutAsJsonAsync(url, body); response.Should().HaveStatusCodeBadRequest() - .And.Should().HaveBodyAsCommonResponseWithCode(CommonResponse.ErrorCodes.InvalidModel); + .And.Should().HaveBodyAsCommonResponseWithCode(ErrorCodes.Http.Common.InvalidModel); } } } diff --git a/Timeline.Tests/Helpers/MyTestLoggerFactory.cs b/Timeline.Tests/Helpers/MyTestLoggerFactory.cs deleted file mode 100644 index b9960378..00000000 --- a/Timeline.Tests/Helpers/MyTestLoggerFactory.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Xunit.Abstractions; - -namespace Timeline.Tests.Helpers -{ - public static class Logging - { - public static ILoggerFactory Create(ITestOutputHelper outputHelper) - { - // TODO: Use test output. - return NullLoggerFactory.Instance; - } - - public static IWebHostBuilder ConfigureTestLogging(this IWebHostBuilder builder) - { - builder.ConfigureLogging(logging => - { - //logging.AddXunit(outputHelper); - }); - return builder; - } - } -} 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); - }); - }); - }); - } - } -} diff --git a/Timeline.Tests/Helpers/TestApplication.cs b/Timeline.Tests/Helpers/TestApplication.cs new file mode 100644 index 00000000..b0187a30 --- /dev/null +++ b/Timeline.Tests/Helpers/TestApplication.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using System; +using Timeline.Entities; +using Timeline.Tests.Mock.Data; + +namespace Timeline.Tests.Helpers +{ + public class TestApplication : IDisposable + { + public SqliteConnection DatabaseConnection { get; } = new SqliteConnection("Data Source=:memory:;"); + public WebApplicationFactory Factory { get; } + + public TestApplication(WebApplicationFactory factory) + { + // 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 . + DatabaseConnection.Open(); + + { + var options = new DbContextOptionsBuilder() + .UseSqlite(DatabaseConnection) + .Options; + + using (var context = new DatabaseContext(options)) + { + TestDatabase.InitDatabase(context); + }; + } + + Factory = factory.WithWebHostBuilder(builder => + { + builder.ConfigureServices(services => + { + services.AddEntityFrameworkSqlite(); + services.AddDbContext(options => + { + options.UseSqlite(DatabaseConnection); + }); + }); + }); + } + + public void Dispose() + { + DatabaseConnection.Close(); + DatabaseConnection.Dispose(); + } + } +} -- cgit v1.2.3