单元测试:增加GroupsController单元测试

This commit is contained in:
Argo-Surface 2019-01-16 13:20:26 +08:00
parent 969162a4ef
commit e6c5639150
3 changed files with 72 additions and 4 deletions

View File

@ -63,7 +63,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="type"></param> /// <param name="type"></param>
/// <returns></returns> /// <returns></returns>
[HttpPost("{id}")] [HttpPost("{id}")]
public IEnumerable<object> Post(string id, [FromQuery]string type) public IEnumerable<Group> Post(string id, [FromQuery]string type)
{ {
IEnumerable<Group> ret = new List<Group>(); IEnumerable<Group> ret = new List<Group>();
switch (type) switch (type)
@ -77,7 +77,7 @@ namespace Bootstrap.Admin.Controllers.Api
default: default:
break; break;
} }
return ret.Select(p => new { p.Id, p.Checked, p.GroupName, p.Description }); return ret;
} }
/// <summary> /// <summary>

View File

@ -7,9 +7,9 @@ namespace Bootstrap.Admin.Api
{ {
protected HttpClient Client { get; } protected HttpClient Client { get; }
public ApiTest(BAWebHost factory, string view, bool login) public ApiTest(BAWebHost factory, string controller, bool login)
{ {
factory.ClientOptions.BaseAddress = new System.Uri($"http://localhost/api/{view}"); factory.ClientOptions.BaseAddress = new System.Uri($"http://localhost/api/{controller}/");
Client = factory.CreateClient(); Client = factory.CreateClient();
if (login) factory.LoginAsync(Client).GetAwaiter(); if (login) factory.LoginAsync(Client).GetAwaiter();

View File

@ -0,0 +1,68 @@
using Longbow.Web.Mvc;
using Xunit;
using Bootstrap.DataAccess;
using System.Linq;
using System.Collections.Generic;
namespace Bootstrap.Admin.Api
{
public class GroupsTest : ApiTest
{
public GroupsTest(BAWebHost factory) : base(factory, "Groups", true)
{
}
[Fact]
public async void Get_Ok()
{
// 菜单 系统菜单 系统使用条件
var query = "?sort=GroupName&order=asc&offset=0&limit=20&groupName=Admin&description=%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E7%BB%84&_=1547614230481";
var qd = await Client.GetAsJsonAsync<QueryData<Group>>(query);
Assert.Single(qd.rows);
}
[Fact]
public async void GetById_Ok()
{
var id = new Group().Retrieves().Where(gp => gp.GroupName == "Admin").First().Id;
var g = await Client.GetAsJsonAsync<Group>(id);
Assert.Equal("Admin", g.GroupName);
}
[Fact]
public async void PostAndDelete_Ok()
{
var ret = await Client.PostAsJsonAsync<Group, bool>("", new Group() { GroupName = "UnitTest-Group", Description = "UnitTest-Desc" });
Assert.True(ret);
var ids = new Group().Retrieves().Where(d => d.GroupName == "UnitTest-Group").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
}
[Fact]
public async void PostById_Ok()
{
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<Group>>($"{uid}?type=user", string.Empty);
Assert.NotEmpty(ret);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<Group>>($"{rid}?type=role", string.Empty);
Assert.NotEmpty(ret);
}
[Fact]
public async void PutById_Ok()
{
var ids = new Group().Retrieves().Select(g => g.Id);
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{uid}?type=user", ids);
Assert.True(ret);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}?type=role", ids);
Assert.True(ret);
}
}
}