代码:GitHub
参考地址:https://github.com/Acumatica/xunit.autofac
xunit +autofac进行单元测试
①创建一个类库
引用nuget:
xunit xunit.runner.visualstudio 可以在vs中跑的包 xunit.autofac
②创建一个ServiceRegistration 类
public class ServiceRegistration : Module //Autofac { /// <summary> /// 依赖注入 /// </summary> /// <param name="builder"></param> protected override void Load(ContainerBuilder builder) { builder.RegisterType<JwtUserRepository>().As<IJwtUserRepository>(); builder.RegisterType<JwtUserService>().As<IJwtUserService>(); } }
在程序集中注册
添加测试类:
/// <summary> /// Jwt用户表应用层服务测试 /// </summary> public class JwtUserAppService_Test { /// <summary> /// 依赖注入 /// </summary> private readonly IJwtUserService _jwtUserService; public JwtUserAppService_Test(IJwtUserService jwtUserService) { this._jwtUserService = jwtUserService; } /// <summary> /// 全部用户 /// </summary> /// <returns></returns> [Fact] public async Task GetListAsync_Test() { List<JwtUser> list = await this._jwtUserService.GetListAsync(); } /// <summary> /// 用户名和密码获取用户 /// </summary> /// <param name="inDto">inDto</param> /// <returns>OutDto</returns> [Fact] public async Task GetJwtUserAsync_Test() { AuthenticateInDto inDto = new AuthenticateInDto { UserName = "sealee", PassWord = "123" }; JwtUser model = await this._jwtUserService.GetJwtUserAsync(inDto); } //添加删除修改,都需要新建一个类进行测试 } }
打开测试: