2022-03-31 23:50:04 +08:00
|
|
|
|
using System;
|
2021-01-27 13:42:38 +08:00
|
|
|
|
using System.IO;
|
2021-04-21 18:02:01 +08:00
|
|
|
|
using System.Linq;
|
2021-01-27 13:42:38 +08:00
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
2021-01-14 23:35:54 +08:00
|
|
|
|
using Infrastructure;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Infrastructure.Extensions.AutofacManager;
|
2021-04-21 18:02:01 +08:00
|
|
|
|
using Infrastructure.Utilities;
|
2021-01-14 23:35:54 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2021-01-27 13:42:38 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using OpenAuth.Repository;
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App.Test
|
|
|
|
|
{
|
|
|
|
|
public class TestBase
|
|
|
|
|
{
|
|
|
|
|
protected AutofacServiceProvider _autofacServiceProvider;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
var serviceCollection = GetService();
|
|
|
|
|
serviceCollection.AddMemoryCache();
|
|
|
|
|
serviceCollection.AddOptions();
|
2021-01-27 13:42:38 +08:00
|
|
|
|
//读取OpenAuth.WebApi的配置文件用于单元测试
|
|
|
|
|
var path = AppContext.BaseDirectory;
|
2022-03-31 23:50:04 +08:00
|
|
|
|
int pos = path.IndexOf("OpenAuth.App");
|
|
|
|
|
if (pos == -1) //如果测试入口是OpenAuth.WebApi
|
|
|
|
|
{
|
|
|
|
|
pos = path.IndexOf("OpenAuth.WebApi");
|
|
|
|
|
}
|
2021-01-27 13:42:38 +08:00
|
|
|
|
var basepath = Path.Combine(path.Substring(0,pos) ,"OpenAuth.WebApi");
|
|
|
|
|
IConfiguration config = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(basepath)
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
|
|
|
.AddJsonFile("appsettings.Development.json", optional: true)
|
|
|
|
|
.AddEnvironmentVariables()
|
|
|
|
|
.Build();
|
2021-04-21 18:02:01 +08:00
|
|
|
|
|
2021-03-09 23:35:14 +08:00
|
|
|
|
serviceCollection.Configure<AppSetting>(config.GetSection("AppSetting"));
|
2021-01-27 13:42:38 +08:00
|
|
|
|
//添加log4net
|
|
|
|
|
serviceCollection.AddLogging(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.ClearProviders(); //去掉默认的日志
|
|
|
|
|
builder.AddConfiguration(config.GetSection("Logging")); //读取配置文件中的Logging配置
|
|
|
|
|
builder.AddLog4Net();
|
|
|
|
|
});
|
|
|
|
|
//注入OpenAuth.WebApi配置文件
|
|
|
|
|
serviceCollection.AddScoped(x => config);
|
2021-01-14 23:35:54 +08:00
|
|
|
|
|
2021-01-27 13:42:38 +08:00
|
|
|
|
//模拟HTTP请求
|
2021-01-14 23:35:54 +08:00
|
|
|
|
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
|
|
|
|
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
|
|
|
|
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
|
|
|
|
|
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
|
2021-01-27 13:42:38 +08:00
|
|
|
|
|
|
|
|
|
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var container = AutofacExt.InitForTest(serviceCollection);
|
|
|
|
|
_autofacServiceProvider = new AutofacServiceProvider(container);
|
|
|
|
|
AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
|
2021-04-21 18:02:01 +08:00
|
|
|
|
|
|
|
|
|
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
|
|
|
|
.ToDictionary(x => x.Key, x => x.Value);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试框架默认只注入了缓存Cache,配置Option;
|
|
|
|
|
/// 如果在测试的过程中需要模拟登录用户,cookie等信息,需要重写该方法,可以参考TestFlow的写法
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual ServiceCollection GetService()
|
|
|
|
|
{
|
|
|
|
|
return new ServiceCollection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|