[英]Tests fail: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered
When I run my E2E tests I get the exception:当我运行 E2E 测试时,出现异常:
No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered.
I've followed the official Autofac documentation of implementing the Autofac in projects using .NET versions 3+ and I can run the solution just fine, but my tests fail.我遵循了在使用 .NET 版本 3+ 的项目中实现 Autofac 的官方 Autofac 文档,我可以很好地运行该解决方案,但我的测试失败了。
What's weird is that when I use a solution code from this topic , it's the opposite - I can't run the solution (returning IServiceProvider is invalid), but my tests succeed:奇怪的是,当我使用本主题中的解决方案代码时,情况正好相反 - 我无法运行该解决方案(返回 IServiceProvider 无效),但我的测试成功了:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.EnableEndpointRouting = false)
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddCustomDbContext(Configuration);
services.AddOptions();
var container = new ContainerBuilder();
container.Populate(services);
return new AutofacServiceProvider(container.Build());
}
What am I doing wrong?我究竟做错了什么?
After a long battle I managed to get it to work, all thanks to this article .经过长时间的战斗,我设法让它工作,这一切都归功于这篇文章。
Your application needs to implement Autofac in .NET 3.0+ way and you need to look at steps "Adding XUnit logging with WebApplicationFactory in ASP.NET Core 2.x" and "Using WebApplicationFactory in ASP.NET Core 3.0" from the article to make tests work, ie add the tests fixture and use it instead to create a client.您的应用程序需要以 .NET 3.0+ 方式实现 Autofac,您需要查看文章中的步骤“在 ASP.NET Core 2.x 中使用 WebApplicationFactory 添加 XUnit 日志记录”和“在 ASP.NET Core 3.0 中使用 WebApplicationFactory”来制作测试工作,即添加测试装置并使用它来创建客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.