-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using SimpleInjector in integration tests via WebApplicationFactory #29
Comments
I tried your code, but it actually works on my machine. Could it be that you didn't call |
I tried to follow documentation https://docs.simpleinjector.org/en/latest/aspnetintegration.html But anyway here is configuration code from the Startup.cs: public class Startup
{
//....
private readonly Container _container = new Container();
//...
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(...);
//...
services.AddSimpleInjector(_container, opts =>
{
opts.AddAspNetCore()
.AddControllerActivation();
opts.AddLogging();
});
SimpleInjectorConfig.Configure(...);
///.....
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSimpleInjector(_container);
// ....
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// ....
_container.Verify(VerificationOption.VerifyAndDiagnose);
}
} |
Try this: public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new SimpleInjector.Container());
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
}
} This adds the container to the collection. If you can't retrieve that instance in your test, there is likely something happening that your code doesn't show. As I said, I tried to reproduce the issue, but a call to |
Thank you, @dotnetjunkie, your snippet helped. Btw, I've looked through the sources and haven't found the exact place where SimpleInjector.Container is being added to the IServiceCollection. Can you, please, point me to the place? |
@dotnetjunkie Probably, somewhere else or this issue has been fixed in a latter version. Anyway, thanks for help, i really appreciate it. |
Ah, that very well could be true. Initially, the container wasn't added. I'm unsure whoch version it was added. But this means that, as long as you're on 4.8, its best to manually add the container to rhe service collection. |
Prerequisites:
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 fromIServiceCollection
(it's simply not there during the.ConfigureTestServices()
).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.
The text was updated successfully, but these errors were encountered: