129 lines
3.9 KiB
C#
129 lines
3.9 KiB
C#
|
using System;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Reflection;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Abp.AspNetCore.TestBase;
|
|||
|
using KDTEC.ProManager.EntityFrameworkCore;
|
|||
|
using KDTEC.ProManager.Tests.TestDatas;
|
|||
|
using KDTEC.ProManager.Web.Controllers;
|
|||
|
using KDTEC.ProManager.Web.Startup;
|
|||
|
using KDTEC.ProManager.Web.Tests.Controllers;
|
|||
|
using AngleSharp.Html.Dom;
|
|||
|
using AngleSharp.Html.Parser;
|
|||
|
using Microsoft.AspNetCore.Hosting;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using Newtonsoft.Json.Serialization;
|
|||
|
using Shouldly;
|
|||
|
|
|||
|
namespace KDTEC.ProManager.Web.Tests
|
|||
|
{
|
|||
|
public abstract class ProManagerWebTestBase : AbpAspNetCoreIntegratedTestBase<Startup>
|
|||
|
{
|
|||
|
protected static readonly Lazy<string> ContentRootFolder;
|
|||
|
|
|||
|
static ProManagerWebTestBase()
|
|||
|
{
|
|||
|
ContentRootFolder = new Lazy<string>(WebContentDirectoryFinder.CalculateContentRootFolder, true);
|
|||
|
}
|
|||
|
|
|||
|
protected ProManagerWebTestBase()
|
|||
|
{
|
|||
|
UsingDbContext(context => new TestDataBuilder(context).Build());
|
|||
|
}
|
|||
|
|
|||
|
protected override IWebHostBuilder CreateWebHostBuilder()
|
|||
|
{
|
|||
|
return base
|
|||
|
.CreateWebHostBuilder()
|
|||
|
.UseContentRoot(ContentRootFolder.Value)
|
|||
|
.UseSetting(WebHostDefaults.ApplicationKey, typeof(ProManagerWebModule).Assembly.FullName);
|
|||
|
}
|
|||
|
|
|||
|
#region Get response
|
|||
|
|
|||
|
protected async Task<T> GetResponseAsObjectAsync<T>(string url,
|
|||
|
HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
|
|||
|
{
|
|||
|
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode);
|
|||
|
return JsonConvert.DeserializeObject<T>(strResponse, new JsonSerializerSettings
|
|||
|
{
|
|||
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
protected async Task<string> GetResponseAsStringAsync(string url,
|
|||
|
HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
|
|||
|
{
|
|||
|
var response = await GetResponseAsync(url, expectedStatusCode);
|
|||
|
return await response.Content.ReadAsStringAsync();
|
|||
|
}
|
|||
|
|
|||
|
protected async Task<HttpResponseMessage> GetResponseAsync(string url,
|
|||
|
HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
|
|||
|
{
|
|||
|
var response = await Client.GetAsync(url);
|
|||
|
response.StatusCode.ShouldBe(expectedStatusCode);
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region UsingDbContext
|
|||
|
|
|||
|
protected void UsingDbContext(Action<ProManagerDbContext> action)
|
|||
|
{
|
|||
|
using (var context = IocManager.Resolve<ProManagerDbContext>())
|
|||
|
{
|
|||
|
action(context);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
protected T UsingDbContext<T>(Func<ProManagerDbContext, T> func)
|
|||
|
{
|
|||
|
T result;
|
|||
|
|
|||
|
using (var context = IocManager.Resolve<ProManagerDbContext>())
|
|||
|
{
|
|||
|
result = func(context);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
protected async Task UsingDbContextAsync(Func<ProManagerDbContext, Task> action)
|
|||
|
{
|
|||
|
using (var context = IocManager.Resolve<ProManagerDbContext>())
|
|||
|
{
|
|||
|
await action(context);
|
|||
|
await context.SaveChangesAsync(true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
protected async Task<T> UsingDbContextAsync<T>(Func<ProManagerDbContext, Task<T>> func)
|
|||
|
{
|
|||
|
T result;
|
|||
|
|
|||
|
using (var context = IocManager.Resolve<ProManagerDbContext>())
|
|||
|
{
|
|||
|
result = await func(context);
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region ParseHtml
|
|||
|
|
|||
|
protected IHtmlDocument ParseHtml(string htmlString)
|
|||
|
{
|
|||
|
return new HtmlParser().ParseDocument(htmlString);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|