【发布时间】:2021-11-01 14:54:05
【问题描述】:
我正在向我的tests 发送fixture。
在我的FixtureFactory 中,我正在制作我自己的ServiceCollection:
private static ServiceCollection CreateServiceCollection()
{
var services = new ServiceCollection();
services.AddScoped<IConfiguration>();
services.AddScoped<ITokenService, TokenService>();
services.AddDbContext<TaskManagerDbContext>(o => o.UseInMemoryDatabase(Guid.NewGuid().ToString()));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<TaskManagerDbContext>();
return services;
}
然后,我从scope 得到这个services:
var services = CreateServiceCollection().BuildServiceProvider().CreateScope();
var context = services.ServiceProvider.GetRequiredService<TaskManagerDbContext>();
var userManager = services.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var signInManager = services.ServiceProvider.GetRequiredService<SignInManager<ApplicationUser>>();
var tokenService = services.ServiceProvider.GetRequiredService<TokenService>();
当我将TokenService 添加到我的ServiceCollection 时,问题就开始了。
TokenService 的构造函数如下所示:
private readonly IConfiguration configuration;
private readonly UserManager<ApplicationUser> userManager;
public TokenService(IConfiguration configuration, UserManager<ApplicationUser> userManager)
{
this.configuration = configuration;
this.userManager = userManager;
}
当我启动我的Core 应用程序时它工作得很好,但不能从我上面提到的ServiceCollection 工作。
在测试中,我开始收到此错误:
消息:System.AggregateException:发生一个或多个错误。 (无法为服务类型“Microsoft.Extensions.Configuration.IConfiguration”实例化实现类型“Microsoft.Extensions.Configuration.IConfiguration”。)(以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具) ---- System.ArgumentException:无法为服务类型“Microsoft.Extensions.Configuration.IConfiguration”实例化实现类型“Microsoft.Extensions.Configuration.IConfiguration”。 ---- 以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具
当我将services.AddScoped<IConfiguration>(); 添加到我的ServiceCollection 以及当我开始获得所需的服务TokenService 时,此错误开始出现。
我的问题是,如何正确注册我在Service 中使用的Configuration?
我正在使用Configuration 从settings.json 获取项目。
编辑:
我的fixture 和示例测试类:
public class ServicesFixture
{
public TaskManagerDbContext Context { get; private set; }
public UserManager<ApplicationUser> UserManager { get; private set; }
public RoleManager<IdentityRole> RoleManager { get; private set; }
public SignInManager<ApplicationUser> SignInManager { get; private set; }
public TokenService TokenService { get; private set; }
public ServicesFixture()
{
var servicesModel = ServicesFactory.CreateProperServices();
this.Context = servicesModel.Context;
this.UserManager = servicesModel.UserManager;
this.RoleManager = servicesModel.RoleManager;
this.SignInManager = servicesModel.SignInManager;
this.TokenService = servicesModel.TokenService;
}
[CollectionDefinition("ServicesTestCollection")]
public class QueryCollection : ICollectionFixture<ServicesFixture>
{
}
}
测试:
[Collection("ServicesTestCollection")]
public class CreateProjectCommandTests
{
private readonly TaskManagerDbContext context;
public CreateProjectCommandTests(ServicesFixture fixture)
{
this.context = fixture.Context;
}
}
EDIT2
当我从我的集合中删除 AddScoped<IConfiguration>(); 并运行测试时,我收到此错误:
消息:System.AggregateException:发生一个或多个错误。 (未注册类型“TaskManager.Infrastructure.Implementations.TokenService”的服务。)(以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具) ---- System.InvalidOperationException:没有注册类型“TaskManager.Infrastructure.Implementations.TokenService”的服务。 ---- 以下构造函数参数没有匹配的夹具数据:ServicesFixture 夹具
【问题讨论】:
-
您添加了
IConfiguration,但没有实现。 -
在
Startup.cs配置刚刚注入。如何实现Configuration? -
您也可以只为
IConfiguration创建一个Mock并使用它。您实际上并不需要为单元测试创建IServicesCollection。 -
我想使用我在
WebApi项目中使用的现有设置文件(在这种情况下,默认为appsettings.json) -
或者无论如何,我可以制作另一个
settings.json文件并在我自己的ServiceCollection中在我的Configuration上实现它。settings文件不是问题 tbh
标签: c# asp.net-core dependency-injection .net-core asp.net-core-2.2