简体   繁体   中英

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:

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM