From 26392cdd2028eff2132948d5cc5e36e08a2d69a9 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 11 Aug 2019 15:34:59 +0800 Subject: Add FluentAssertions. --- Timeline.Tests/UserUnitTest.cs | 52 ++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) (limited to 'Timeline.Tests/UserUnitTest.cs') diff --git a/Timeline.Tests/UserUnitTest.cs b/Timeline.Tests/UserUnitTest.cs index c5c91d34..054d9caa 100644 --- a/Timeline.Tests/UserUnitTest.cs +++ b/Timeline.Tests/UserUnitTest.cs @@ -1,3 +1,4 @@ +using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; using System.Net; using System.Net.Http; @@ -28,15 +29,9 @@ namespace Timeline.Tests using (var client = await _factory.CreateClientAsAdmin()) { var res = await client.GetAsync("users"); - Assert.Equal(HttpStatusCode.OK, res.StatusCode); - // Because tests are running asyncronized. So database may be modified and // we can't check the exact user lists at this point. So only check the format. - - // var users = (await res.ReadBodyAsJson()).ToList(); - // users.Sort(UserInfoComparers.Comparer); - // Assert.Equal(MockUsers.UserInfos, users, UserInfoComparers.EqualityComparer); - await res.ReadBodyAsJson(); + res.Should().HaveStatusCodeOk().And.Should().HaveBodyAsJson(); } } @@ -46,9 +41,9 @@ namespace Timeline.Tests using (var client = await _factory.CreateClientAsAdmin()) { var res = await client.GetAsync("users/" + MockUsers.UserUsername); - res.AssertOk(); - var user = await res.ReadBodyAsJson(); - Assert.Equal(MockUsers.UserUserInfo, user, UserInfoComparers.EqualityComparer); + res.Should().HaveStatusCodeOk() + .And.Should().HaveBodyAsJson() + .Which.Should().BeEquivalentTo(MockUsers.UserUserInfo); } } @@ -58,9 +53,8 @@ namespace Timeline.Tests using (var client = await _factory.CreateClientAsAdmin()) { var res = await client.GetAsync("users/usernotexist"); - res.AssertNotFound(); - var body = await res.ReadBodyAsJson(); - Assert.Equal(UserController.ErrorCodes.Get_NotExist, body.Code); + res.Should().HaveStatusCodeNotFound() + .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Get_NotExist); } } @@ -80,9 +74,9 @@ namespace Timeline.Tests async Task CheckAdministrator(bool administrator) { var res = await client.GetAsync(url); - res.AssertOk(); - var body = await res.ReadBodyAsJson(); - Assert.Equal(administrator, body.Administrator); + res.Should().HaveStatusCodeOk() + .And.Should().HaveBodyAsJson() + .Which.Administrator.Should().Be(administrator); } { @@ -92,7 +86,7 @@ namespace Timeline.Tests Password = password, Administrator = false }); - await res.AssertIsPutCreated(); + res.Should().BePutCreated(); await CheckAdministrator(false); } @@ -103,38 +97,37 @@ namespace Timeline.Tests Password = password, Administrator = true }); - await res.AssertIsPutModified(); + res.Should().BePutModified(); await CheckAdministrator(true); } // Patch Not Exist { var res = await client.PatchAsJsonAsync("users/usernotexist", new UserPatchRequest { }); - res.AssertNotFound(); - var body = await res.ReadBodyAsJson(); - Assert.Equal(UserController.ErrorCodes.Patch_NotExist, body.Code); + res.Should().HaveStatusCodeNotFound() + .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.Patch_NotExist); } // Patch Success { var res = await client.PatchAsJsonAsync(url, new UserPatchRequest { Administrator = false }); - res.AssertOk(); + res.Should().HaveStatusCodeOk(); await CheckAdministrator(false); } // Delete Deleted { var res = await client.DeleteAsync(url); - await res.AssertIsDeleteDeleted(); + res.Should().BeDeleteDeleted(); var res2 = await client.GetAsync(url); - res2.AssertNotFound(); + res2.Should().HaveStatusCodeNotFound(); } // Delete Not Exist { var res = await client.DeleteAsync(url); - await res.AssertIsDeleteNotExist(); + res.Should().BeDeleteNotExist(); } } } @@ -176,9 +169,8 @@ namespace Timeline.Tests using (var client = await _factory.CreateClientAsUser()) { var res = await client.PostAsJsonAsync(url, new ChangePasswordRequest { OldPassword = "???", NewPassword = "???" }); - res.AssertBadRequest(); - var body = await res.ReadBodyAsJson(); - Assert.Equal(UserController.ErrorCodes.ChangePassword_BadOldPassword, body.Code); + res.Should().HaveStatusCodeBadRequest() + .And.Should().HaveBodyAsCommonResponseWithCode(UserController.ErrorCodes.ChangePassword_BadOldPassword); } } @@ -192,14 +184,14 @@ namespace Timeline.Tests using (var client = await _factory.CreateClientAsAdmin()) { var res = await client.PutAsJsonAsync("users/" + username, new UserPutRequest { Password = password, Administrator = false }); - Assert.Equal(HttpStatusCode.Created, res.StatusCode); + res.Should().BePutCreated(); } using (var client = await _factory.CreateClientWithCredential(username, password)) { const string newPassword = "new"; var res = await client.PostAsJsonAsync(url, new ChangePasswordRequest { OldPassword = password, NewPassword = newPassword }); - res.AssertOk(); + res.Should().HaveStatusCodeOk(); await client.CreateUserTokenAsync(username, newPassword); } } -- cgit v1.2.3 From ec3743db2079e88a184c49564a7f46bbddf4b03e Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 11 Aug 2019 15:36:06 +0800 Subject: Clean code. --- Timeline.Tests/AuthorizationUnitTest.cs | 2 +- Timeline.Tests/TokenUnitTest.cs | 4 +--- Timeline.Tests/UserUnitTest.cs | 1 - 3 files changed, 2 insertions(+), 5 deletions(-) (limited to 'Timeline.Tests/UserUnitTest.cs') diff --git a/Timeline.Tests/AuthorizationUnitTest.cs b/Timeline.Tests/AuthorizationUnitTest.cs index 3df4d0fd..6f52a12d 100644 --- a/Timeline.Tests/AuthorizationUnitTest.cs +++ b/Timeline.Tests/AuthorizationUnitTest.cs @@ -1,3 +1,4 @@ +using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; using System.Net; using System.Threading.Tasks; @@ -5,7 +6,6 @@ using Timeline.Tests.Helpers; using Timeline.Tests.Helpers.Authentication; using Xunit; using Xunit.Abstractions; -using FluentAssertions; namespace Timeline.Tests { diff --git a/Timeline.Tests/TokenUnitTest.cs b/Timeline.Tests/TokenUnitTest.cs index 86f51c0b..b5d8a2c8 100644 --- a/Timeline.Tests/TokenUnitTest.cs +++ b/Timeline.Tests/TokenUnitTest.cs @@ -1,8 +1,7 @@ +using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; -using Newtonsoft.Json; using System; -using System.Net; using System.Net.Http; using Timeline.Controllers; using Timeline.Models.Http; @@ -13,7 +12,6 @@ using Timeline.Tests.Mock.Data; using Timeline.Tests.Mock.Services; using Xunit; using Xunit.Abstractions; -using FluentAssertions; namespace Timeline.Tests { diff --git a/Timeline.Tests/UserUnitTest.cs b/Timeline.Tests/UserUnitTest.cs index 054d9caa..1f72000c 100644 --- a/Timeline.Tests/UserUnitTest.cs +++ b/Timeline.Tests/UserUnitTest.cs @@ -1,6 +1,5 @@ using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; -using System.Net; using System.Net.Http; using System.Threading.Tasks; using Timeline.Controllers; -- cgit v1.2.3