OpenAuth.Net/OpenAuth.Repository/Test/TestBase.cs

53 lines
1.8 KiB
C#
Raw Normal View History

using System.Reflection;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using OpenAuth.Repository.Interface;
namespace OpenAuth.Repository.Test
{
2020-12-17 23:04:04 +08:00
/// <summary>
/// Repository测试基类
/// 测试用于测试DbContext、UnitWork、Repository如果需要测试业务逻辑请使用OpenAuth.App里面的单元测试
/// </summary>
public class TestBase
{
protected AutofacServiceProvider _autofacServiceProvider;
[SetUp]
public void Init()
{
var serviceCollection = GetService();
serviceCollection.AddMemoryCache();
serviceCollection.AddOptions();
serviceCollection.AddScoped(typeof(IRepository<,>), typeof(BaseRepository<,>));
serviceCollection.AddScoped(typeof(IUnitWork<>), typeof(UnitWork<>));
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
var builder = new ContainerBuilder();
//注册repository层
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
builder.Populate(serviceCollection);
var _container = builder.Build();
_autofacServiceProvider = new AutofacServiceProvider(_container);
}
/// <summary>
/// 测试框架默认只注入了缓存Cache配置Option
/// 如果在测试的过程中需要模拟登录用户cookie等信息需要重写该方法可以参考TestFlow的写法
/// </summary>
public virtual ServiceCollection GetService()
{
return new ServiceCollection();
}
}
}