mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 10:37:55 +08:00
同步OpenAuth.Core,单元测试使用WebApi配置文件
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Infrastructure.Extensions.AutofacManager;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using OpenAuth.App.SSO;
|
||||
using OpenAuth.Repository;
|
||||
|
||||
namespace OpenAuth.App.Test
|
||||
@@ -24,31 +23,36 @@ namespace OpenAuth.App.Test
|
||||
var serviceCollection = GetService();
|
||||
serviceCollection.AddMemoryCache();
|
||||
serviceCollection.AddOptions();
|
||||
serviceCollection.AddLogging();
|
||||
|
||||
//模拟配置文件
|
||||
var optionMock = new Mock<IOptions<AppSetting>>();
|
||||
optionMock.Setup(x => x.Value).Returns(new AppSetting { DbType = Define.DBTYPE_MYSQL});
|
||||
serviceCollection.AddScoped(x => optionMock.Object);
|
||||
//读取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"]}");
|
||||
|
||||
//模拟多租户id
|
||||
var configMock = new Mock<IConfiguration>();
|
||||
configMock.Setup(x => x.GetSection("ConnectionStrings")[Define.TENANT_ID]).Returns("");
|
||||
serviceCollection.AddScoped(x => configMock.Object);
|
||||
//添加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<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);
|
||||
|
||||
// 测试my sql
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
options.UseMySql("server=127.0.0.1;user id=root;database=openauthdb;password=000000"));
|
||||
|
||||
// serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
// options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
|
||||
|
||||
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
||||
|
||||
var container = AutofacExt.InitForTest(serviceCollection);
|
||||
_autofacServiceProvider = new AutofacServiceProvider(container);
|
||||
AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
|
||||
|
Reference in New Issue
Block a user