Support integration tests in .NET Core generic hosting
See original GitHub issueProblem Statement
Tragically there’s an issue with .NET Core that means that, by default ConfigureTestContainer doesn’t work. See https://github.com/dotnet/aspnetcore/issues/14907
The unsealed ContainerBuilder is useful for working around this shortcoming in ASP.NET Core.
Unfortunately it was sealed in https://github.com/autofac/Autofac/issues/1120
My fear is that without this workaround being available, and without knowing when (or indeed if) it will be fixed in .NET this may end up being a blocker to upgrade from .NET Core 3.1 to .NET 5.
As I see it, those that write integration tests and use AutoFac are going to be unable to upgrade to .NET 5 without first migrating away from AutoFac or forking it. Neither of those is a great outcome. But it’s going to be that or deactivate a whole suite of tests (and that’s not something that’s likely going be possible).
Thanks for all your work on AutoFac BTW - as an open source maintainer myself I appreciate it’s a lot of hard work 🌻❤️
Desired Solution
Could I make an appeal for ContainerBuilder to be unsealed please @tillig?
Alternatives You’ve Considered / Additional Context
My preference would be for this not to be necessary because it’s remedied in .NET itself. My fear (and the motivation for raising this) is imagining the outcome of people finding themselves having to choose between using AutoFac 5.0 forever or give up on integration tests.
cc @davidfowl @Tratcher @anurse @javiercn - I realise you’re very busy people, but it would be tremendous if this could be considered for fixing in .NET generally so such workarounds wouldn’t be necessary. The integration testing that .NET generally supports is tremendous - thanks for your work on it 🤗
You can see more details in the linked issue and of the workaround in my blog post here:
https://blog.johnnyreilly.com/2020/05/autofac-webapplicationfactory-and.html
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:32 (9 by maintainers)
Top Related StackOverflow Question
It doesn’t work for me either when using AutoFac Modules because they get called last even after ConfigureTestServices.
Workaround I am using is following.
Then define extension method similar to this one
And finally I can use it in XUnit test with ITestFixture
Disadvantage is that you “pollute” your Startup class a bit but I think if it’s a concern to you you can do these modifications in some derived TestStartup class for example.
I may be missing something here, but it should be possible to use a similar workaround without needing to override the
ContainerBuilder. I just tried this:This then works:
@johnnyreilly, perhaps you could try out my alternate workaround and see if I’ve forgotten something?