我正在为我的 fixture
制作 tests
。
在我的 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;
}
然后,我从
services
得到这个 scope
: 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
中使用。在测试中,我开始收到此错误:
当我将
services.AddScoped<IConfiguration>();
添加到我的 ServiceCollection
以及当我开始获得所需的服务 TokenService
时,此错误开始显示。我的问题是,如何正确注册我在
Configuration
中使用的 Service
?我使用
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;
}
}
编辑2
当我从我的集合中删除
AddScoped<IConfiguration>();
并运行测试时,我收到此错误:最佳答案
您添加的IConfiguration
没有实现。
在startup.cs中,框架将在后台创建一个实现并将其添加。您必须使用ConfigurationBuilder
进行相同的操作
var builder = new ConfigurationBuilder()
//.SetBasePath("path here") //<--You would need to set the path
.AddJsonFile("appsettings.json"); //or what ever file you have the settings
IConfiguration configuration = builder.Build();
services.AddScoped<IConfiguration>(_ => configuration);
//...
关于c# - 在自己的 ServiceCollection 中获取 IConfiguration,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57115707/