using System; using System.IO; using Autofac.Extensions.DependencyInjection; using Infrastructure; using Infrastructure.Extensions.AutofacManager; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; 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(); //读取OpenAuth.WebApi的配置文件用于单元测试 var path = AppContext.BaseDirectory; int pos = path.IndexOf("OpenAuth.App"); 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(); Console.WriteLine($"单元测试数据库信息:{config.GetSection("AppSetting")["DbType"]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}"); //添加log4net serviceCollection.AddLogging(builder => { builder.ClearProviders(); //去掉默认的日志 builder.AddConfiguration(config.GetSection("Logging")); //读取配置文件中的Logging配置 builder.AddLog4Net(); }); //注入OpenAuth.WebApi配置文件 serviceCollection.AddScoped(x => config); //模拟HTTP请求 var httpContextAccessorMock = new Mock(); 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); serviceCollection.AddDbContext(); var container = AutofacExt.InitForTest(serviceCollection); _autofacServiceProvider = new AutofacServiceProvider(container); AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider); } /// /// 测试框架默认只注入了缓存Cache,配置Option; /// 如果在测试的过程中需要模拟登录用户,cookie等信息,需要重写该方法,可以参考TestFlow的写法 /// public virtual ServiceCollection GetService() { return new ServiceCollection(); } } }