我使用Asp.net core 8进行了以下NUnit测试.
public class DevextremeTests
{
private WebApplicationFactory<Program> _factory;
public DevextremeTests()
{
_factory = new WebApplicationFactory<Program>();
}
[Test]
public void Can_get_grid()
{
using (var scope = _factory.Services.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var companyServices = scopedServices.GetRequiredService<CompanyServices>(); //not null
var autoQuery = scopedServices.GetRequiredService<IAutoQueryDb>(); //not null
// companyServices.AutoQuery is null
}
}
}
companyServices
人,但该服务上还有其他服务,例如
[FromServices]
public IAutoQueryDb AutoQuery { get; set; } //null
[FromServices]
public ApplicationDbContext Ef { get; set; } //null
在主应用程序中,嵌套服务加载良好.
如果我从使用[FromServices]
切换到构造函数注入,那么它就会工作并且嵌套服务会填充.
为什么[FromServices]
不能与WebApplicationFactory<Program>()
一起工作?