feat: 完成单元测试搭建

This commit is contained in:
橙子
2025-02-23 01:41:31 +08:00
parent f6b19ec2a5
commit f9341fd2ac
11 changed files with 86 additions and 175 deletions

View File

@@ -1,44 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
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
namespace Yi.Abp.Test;
public class YiAbpTestWebBase:YiAbpTestBase
{
public class YiAbpTestWebBase:YiAbpTestBase
public HttpContext HttpContext { get; private set; }
public YiAbpTestWebBase():base()
{
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);
}
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);
}
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";
}
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(); }
}
}