单元测试:增加NewController单元测试

This commit is contained in:
Argo-Surface 2019-01-16 15:36:58 +08:00
parent 53bfd3af08
commit eec18bcaf0
3 changed files with 70 additions and 2 deletions

View File

@ -32,7 +32,7 @@ namespace Bootstrap.Admin.Controllers
/// 新用户授权/拒绝接口 /// 新用户授权/拒绝接口
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpPut("{id}")] [HttpPut]
public bool Put([FromBody]User value) public bool Put([FromBody]User value)
{ {
var ret = false; var ret = false;

View File

@ -0,0 +1,68 @@
using Bootstrap.DataAccess;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Bootstrap.Admin.Api
{
public class NewTest : ApiTest
{
public NewTest(BAWebHost factory) : base(factory, "New", true)
{
}
[Fact]
public async void Get_Ok()
{
var nusr = InsertNewUser();
var resp = await Client.GetAsJsonAsync<IEnumerable<object>>();
Assert.NotEmpty(resp);
// 删除新用户
nusr.Delete(new string[] { nusr.Id });
}
[Fact]
public async void Put_Ok()
{
DeleteUnitTestUser();
var nusr = InsertNewUser();
// Approve
nusr.UserStatus = UserStates.ApproveUser;
var resp = await Client.PutAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
// 删除新用户
nusr.Delete(new string[] { nusr.Id });
// Reject
nusr = InsertNewUser();
nusr.UserStatus = UserStates.RejectUser;
resp = await Client.PutAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
// 删除新用户
nusr.Delete(new string[] { nusr.Id });
}
private User InsertNewUser()
{
// 插入新用户
var nusr = new User() { UserName = "UnitTest-Register", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" };
Assert.True(new User().Save(nusr));
return nusr;
}
private void DeleteUnitTestUser()
{
var ids = new User().RetrieveNewUsers().Where(u => u.UserName == "UnitTest-Register").Select(u => u.Id);
new User().Delete(ids);
ids = new User().Retrieves().Where(u => u.UserName == "UnitTest-Register").Select(u => u.Id);
new User().Delete(ids);
}
}
}

View File

@ -7,7 +7,7 @@ namespace Bootstrap.Admin
{ {
public static class HttpClientExtensions public static class HttpClientExtensions
{ {
public static async Task<T> GetAsJsonAsync<T>(this HttpClient client, string requestUri) public static async Task<T> GetAsJsonAsync<T>(this HttpClient client, string requestUri = null)
{ {
var resp = await client.GetAsync(requestUri); var resp = await client.GetAsync(requestUri);
var json = await resp.Content.ReadAsStringAsync(); var json = await resp.Content.ReadAsStringAsync();