单元测试:增加LogsController单元测试

This commit is contained in:
Argo-Surface 2019-01-16 14:19:33 +08:00
parent 75d4e529bd
commit 8cf2633a8d
2 changed files with 34 additions and 11 deletions

View File

@ -3,6 +3,7 @@ using Bootstrap.DataAccess;
using Longbow.Web.Mvc; using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Linq; using System.Linq;
using System.Net;
namespace Bootstrap.Admin.Controllers.Api namespace Bootstrap.Admin.Controllers.Api
{ {
@ -23,16 +24,7 @@ namespace Bootstrap.Admin.Controllers.Api
{ {
return value.RetrieveData(); return value.RetrieveData();
} }
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public Log Get(string id)
{
return LogHelper.Retrieves().FirstOrDefault(t => t.Id == id);
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -42,7 +34,7 @@ namespace Bootstrap.Admin.Controllers.Api
public bool Post([FromBody]Log value) public bool Post([FromBody]Log value)
{ {
value.ClientAgent = Request.Headers["User-Agent"]; value.ClientAgent = Request.Headers["User-Agent"];
value.ClientIp = HttpContext.Connection.RemoteIpAddress.ToString(); value.ClientIp = (HttpContext.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
value.UserName = User.Identity.Name; value.UserName = User.Identity.Name;
return LogHelper.Save(value); return LogHelper.Save(value);
} }

View File

@ -0,0 +1,31 @@
using Bootstrap.DataAccess;
using Longbow.Web.Mvc;
using Xunit;
namespace Bootstrap.Admin.Api
{
public class LogsTest : ApiTest
{
public LogsTest(BAWebHost factory) : base(factory, "Logs", true)
{
}
[Fact]
public async void Get_Ok()
{
// 菜单 系统菜单 系统使用条件
var query = "?sort=LogTime&order=desc&offset=0&limit=20&operateType=&OperateTimeStart=&OperateTimeEnd=&_=1547617573596";
var qd = await Client.GetAsJsonAsync<QueryData<Log>>(query);
Assert.NotEmpty(qd.rows);
}
[Fact]
public async void Post_Ok()
{
Client.DefaultRequestHeaders.Add("user-agent", "UnitTest");
var resp = await Client.PostAsJsonAsync<Log, bool>("", new Log() { CRUD = "UnitTest", RequestUrl = "~/UnitTest" });
Assert.True(resp);
}
}
}