feat: 添加单元测试web端
This commit is contained in:
21
Yi.Abp.Net8/test/Yi.Abp.Test/Demo/HttpUser_Test.cs
Normal file
21
Yi.Abp.Net8/test/Yi.Abp.Test/Demo/HttpUser_Test.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Yi.Abp.Test.Demo
|
||||||
|
{
|
||||||
|
public class HttpUser_Test : YiAbpTestWebBase
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Http_Test()
|
||||||
|
{
|
||||||
|
var httpContext = GetRequiredService<IHttpContextAccessor>();
|
||||||
|
httpContext.HttpContext.Request.Path.ToString().ShouldBe("/test");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ using Xunit;
|
|||||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||||
|
|
||||||
namespace Yi.Abp.Test.Application
|
namespace Yi.Abp.Test.Demo
|
||||||
{
|
{
|
||||||
public class User_Test : YiAbpTestBase
|
public class User_Test : YiAbpTestBase
|
||||||
{
|
{
|
||||||
@@ -18,6 +18,7 @@ namespace Yi.Abp.Test
|
|||||||
.UseAutofac()
|
.UseAutofac()
|
||||||
.ConfigureServices((host, service) =>
|
.ConfigureServices((host, service) =>
|
||||||
{
|
{
|
||||||
|
ConfigureServices(host, service);
|
||||||
service.AddLogging(builder => builder.ClearProviders().AddConsole().AddDebug());
|
service.AddLogging(builder => builder.ClearProviders().AddConsole().AddDebug());
|
||||||
/*application= */
|
/*application= */
|
||||||
service.AddApplicationAsync<YiAbpTestModule>().Wait();
|
service.AddApplicationAsync<YiAbpTestModule>().Wait();
|
||||||
@@ -32,6 +33,10 @@ namespace Yi.Abp.Test
|
|||||||
//host.InitializeAsync().Wait();
|
//host.InitializeAsync().Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public virtual void ConfigureServices(HostBuilderContext host, IServiceCollection service)
|
||||||
|
{
|
||||||
|
}
|
||||||
protected virtual void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder)
|
protected virtual void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder)
|
||||||
{
|
{
|
||||||
configurationBuilder.AddJsonFile("appsettings.json");
|
configurationBuilder.AddJsonFile("appsettings.json");
|
||||||
|
|||||||
44
Yi.Abp.Net8/test/Yi.Abp.Test/YiAbpTestWebBase.cs
Normal file
44
Yi.Abp.Net8/test/Yi.Abp.Test/YiAbpTestWebBase.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using NSubstitute.Extensions;
|
||||||
|
|
||||||
|
namespace Yi.Abp.Test
|
||||||
|
{
|
||||||
|
public class YiAbpTestWebBase:YiAbpTestBase
|
||||||
|
{
|
||||||
|
public HttpContext HttpContext { get; private set; }
|
||||||
|
public YiAbpTestWebBase():base()
|
||||||
|
{
|
||||||
|
HttpContext httpContext = DefaultHttpContextAccessor.CurrentHttpContext;
|
||||||
|
this.ConfigureHttpContext(httpContext);
|
||||||
|
HttpContext = httpContext;
|
||||||
|
IApplicationBuilder app = new ApplicationBuilder(this.ServiceProvider);
|
||||||
|
RequestDelegate httpDelegate = app.Build();
|
||||||
|
httpDelegate.Invoke(httpContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ConfigureServices(HostBuilderContext host, IServiceCollection service)
|
||||||
|
{
|
||||||
|
service.Replace(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(DefaultHttpContextAccessor), ServiceLifetime.Singleton));
|
||||||
|
base.ConfigureServices(host, service);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void ConfigureHttpContext(HttpContext httpContext)
|
||||||
|
{
|
||||||
|
httpContext.Request.Path= "/test";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal class DefaultHttpContextAccessor : IHttpContextAccessor
|
||||||
|
{
|
||||||
|
internal static HttpContext? CurrentHttpContext { get; set; } = new DefaultHttpContext();
|
||||||
|
public HttpContext? HttpContext { get => CurrentHttpContext; set => throw new NotImplementedException(); }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user