aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/Helpers
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-10-17 20:46:57 +0800
committer杨宇千 <crupest@outlook.com>2019-10-17 20:46:57 +0800
commitdef8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4 (patch)
treedc7688d7d2dd5ab28a7e3c553154ee84676f75d2 /Timeline.Tests/Helpers
parent297d0c9029360f1d5334ed843b9b299356740ec1 (diff)
downloadtimeline-def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4.tar.gz
timeline-def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4.tar.bz2
timeline-def8e8dd78812c019a0d6e8e5a3e2de4e82ae3e4.zip
...
Diffstat (limited to 'Timeline.Tests/Helpers')
-rw-r--r--Timeline.Tests/Helpers/AssertionResponseExtensions.cs24
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs6
-rw-r--r--Timeline.Tests/Helpers/InvalidModelTestHelpers.cs5
-rw-r--r--Timeline.Tests/Helpers/MyTestLoggerFactory.cs25
-rw-r--r--Timeline.Tests/Helpers/MyWebApplicationFactory.cs73
-rw-r--r--Timeline.Tests/Helpers/TestApplication.cs52
6 files changed, 74 insertions, 111 deletions
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<CommonResponse>(because, becauseArgs);
}
+ public static AndWhichConstraint<HttpResponseMessage, CommonDataResponse<T>> HaveBodyAsCommonDataResponse<T>(this HttpResponseMessageAssertions assertions, string because = "", params object[] becauseArgs)
+ {
+ return assertions.HaveBodyAsJson<CommonDataResponse<T>>(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<T>(this HttpResponseMessageAssertions assertions, int expected, string because = "", params object[] becauseArgs)
+ {
+ assertions.HaveBodyAsCommonDataResponse<T>(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<CommonPutResponse.ResponseData>(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<CommonPutResponse.ResponseData>(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<CommonDeleteResponse.ResponseData>(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<CommonDeleteResponse.ResponseData>(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<CreateTokenResponse> 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<CreateTokenResponse>(await response.Content.ReadAsStringAsync());
return result;
@@ -29,12 +29,12 @@ namespace Timeline.Tests.Helpers.Authentication
public static Task<HttpClient> CreateClientAsUser<T>(this WebApplicationFactory<T> factory) where T : class
{
- return factory.CreateClientWithCredential(MockUsers.UserUsername, MockUsers.UserPassword);
+ return factory.CreateClientWithCredential(MockUser.User.Username, MockUser.User.Password);
}
public static Task<HttpClient> CreateClientAsAdmin<T>(this WebApplicationFactory<T> 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<T>(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<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
- {
- protected override void ConfigureWebHost(IWebHostBuilder builder)
- {
- builder.ConfigureTestServices(services =>
- {
- services.AddSingleton<IClock, TestClock>();
- });
- }
- }
-
- public static class WebApplicationFactoryExtensions
- {
- public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> 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<DatabaseContext>()
- .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<DatabaseContext>(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<Startup> Factory { get; }
+
+ public TestApplication(WebApplicationFactory<Startup> 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<DatabaseContext>()
+ .UseSqlite(DatabaseConnection)
+ .Options;
+
+ using (var context = new DatabaseContext(options))
+ {
+ TestDatabase.InitDatabase(context);
+ };
+ }
+
+ Factory = factory.WithWebHostBuilder(builder =>
+ {
+ builder.ConfigureServices(services =>
+ {
+ services.AddEntityFrameworkSqlite();
+ services.AddDbContext<DatabaseContext>(options =>
+ {
+ options.UseSqlite(DatabaseConnection);
+ });
+ });
+ });
+ }
+
+ public void Dispose()
+ {
+ DatabaseConnection.Close();
+ DatabaseConnection.Dispose();
+ }
+ }
+}