-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Prerequisites:
- .NET Core 3.1
- Simple Injector 4.8
I need to replace some of the services registered via Simple Injector for test purposes. It could be easily done using net core built-in DI container. But i found it impossible to implement with Simple Injector.
Default extension points in WebApplicationFactory for doing this are either .ConfigureTestServices() or .ConfigureTestContainer().
The latter is not an option considering this issue dotnet/aspnetcore#14907
But I couldn't do it via .ConfigureTestServices() either.
I can not get Container instance from IServiceCollection (it's simply not there during the .ConfigureTestServices()).
public class CustomWebApplicationFactory : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestContainer<Container>(container =>
{
//useless, it's impossible to get here
});
builder.ConfigureTestServices(services =>
{
var container =
(Container)services
// System.InvalidOperationException: Sequence contains no matching element
.Last(d => d.ServiceType == typeof(Container))
.ImplementationInstance;
container.Options.AllowOverridingRegistrations = true;
var testServiceMock = new Mock<ITestService>();
container.Register(() => testServiceMock.Object);
});
}
}Even if I hack the actual Container Instance from Startup.cs and try to register service mock, I got "Container is locked" error.
So, I'd like to know, if there is a way to use Simple Injector in such cases and, if positive, how do I do that.