Search code examples
unit-testingdependency-injectionsimple-injector

Which Simple Injector Scoped lifestyle should I use during unit testing?


I'm writing integration tests that setup a container on each test setup. I'm reusing registration code, but I get an error when attempting to register a dependency with Lifestyle.Scoped I get this error:

SetUp : System.InvalidOperationException : To be able to use the Lifestyle.Scoped property, please ensure that the container is configured with a default scoped lifestyle by setting the Container.Options.DefaultScopedLifestyle property with the required scoped lifestyle for your type of application.

What DefaultScopedLifestyle should I use in my case? The scope should really be a singleton, but remember that I am reusing registration code from the webapi app.


Solution

  • For integration test scenarios the scoped lifestyles that can be used are Per Lifetime Scope and Per Execution Context Scope. Per Lifetime Scope allows defining a scope that is bound to a thread, while Per Execution Contest Scope allows defining a scope that will flow over async operations.