feat: test project

main
NoahLan 7 months ago
parent 8abbd5bea2
commit 4650eb70ac

@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Http;
using Shouldly;
using Xunit;
namespace NPin.Test.Demo;
public class HttpUser_Test : NPinTestWebBase
{
[Fact]
public void Http_Test()
{
var httpContext = GetRequiredService<IHttpContextAccessor>();
httpContext.HttpContext.Request.Path.ToString().ShouldBe("/test");
}
}

@ -0,0 +1,12 @@
using Xunit;
namespace NPin.Test.Demo;
public class User_Test: NPinTestBase
{
[Fact]
public async Task Get_User_List_Test()
{
// var service = GetRequiredService<IUserSer>()
}
}

@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="Volo.Abp.TestBase" Version="$(AbpVersion)" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.extensibility.execution" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NPin.Application\NPin.Application.csproj" />
<ProjectReference Include="..\..\src\NPin.SqlSugarCore\NPin.SqlSugarCore.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,42 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace NPin.Test;
public class NPinTestBase : AbpTestBaseWithServiceProvider
{
public ILogger Logger { get; private set; }
protected IServiceScope TestServiceScope { get; }
public NPinTestBase()
{
IHost host = Host.CreateDefaultBuilder()
.UseAutofac()
.ConfigureServices((host, service) =>
{
ConfigureServices(host, service);
service.AddLogging(builder => builder.ClearProviders().AddConsole().AddDebug());
service.AddApplicationAsync<NPinTestModule>().Wait();
})
.ConfigureAppConfiguration(ConfigureAppConfiguration)
.Build();
ServiceProvider = host.Services;
TestServiceScope = ServiceProvider.CreateScope();
Logger = (ILogger)ServiceProvider.GetRequiredService(typeof(ILogger<>).MakeGenericType(GetType()));
host.InitializeAsync().Wait();
}
public virtual void ConfigureServices(HostBuilderContext host, IServiceCollection service)
{
}
protected virtual void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder)
{
configurationBuilder.AddJsonFile("appsettings.json");
}
}

@ -0,0 +1,19 @@
using NPin.Application;
using NPin.SqlSugarCore;
using Volo.Abp.Auditing;
using Volo.Abp.Autofac;
namespace NPin.Test;
[DependsOn(
typeof(NPinSqlSugarCoreModule),
typeof(NPinApplicationModule),
typeof(AbpAutofacModule),
typeof(AbpAuditingModule)
)]
public class NPinTestModule : AbpTestBaseModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
}
}

@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
namespace NPin.Test;
public class NPinTestWebBase : NPinTestBase
{
public HttpContext HttpContext { get; private set; }
public NPinTestWebBase() : base()
{
var httpContext = DefaultHttpContextAccessor.CurrentHttpContext;
ConfigureHttpContext(httpContext);
HttpContext = httpContext;
var app = new ApplicationBuilder(ServiceProvider);
var 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();
}
}

@ -0,0 +1,71 @@
{
"Logging": {
"LogLevel": {
//"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
//
"App": {
"SelfUrl": "http://*:19001",
"CorsOrigins": "http://localhost:19001;http://localhost:18000"
},
//
"DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ],
"DbConnOptions": {
"Url": "DataSource=npin-dev.db",
"DbType": "Sqlite",
"EnabledReadWrite": false,
"EnabledCodeFirst": true,
"EnabledSqlLog": true,
"EnabledDbSeed": true
//
//"ReadUrl": [
// "DataSource=[xxxx]", //Sqlite
// "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", //Mysql
// "Data Source=[xxxx];Initial Catalog=[xxxx];User ID=[xxxx];password=[xxxx]" //Sqlserver
//]
},
//
"JwtOptions": {
"Issuer": "NoahLan",
"Audience": "NoahLan",
"SecurityKey": "zqxwcevrbtnymu312412ihe9rfwhe78rh23djoi32hrui3ryf9e8wfh34iuj54y0934uti4h97fgw7hf97wyh8yy69520",
"ExpiresMinuteTime": 86400
},
//
"OAuth": {
//QQ
"QQ": {
"ClientId": "",
"ClientSecret": "",
"RedirectUri": ""
},
//
"Gitee": {
"ClientId": "",
"ClientSecret": "",
"RedirectUri": ""
}
},
//Rbac
"RbacOptions": {
//
"AdminPassword": "123456",
//
"EnableCaptcha": true,
//
"EnableRegister": false,
//
"EnableDataBaseBackup": false
}
}
Loading…
Cancel
Save