Reputation: 1039
How can I unit test an injection for WcfOperationLifestyle container? I have in my MyBootstrapper class
container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
...
container.Register<IService, Service>(Lifestyle.Scoped);
according to http://simpleinjector.readthedocs.io/en/latest/wcfintegration.html ,but when I unit test
var actual = MyBootstrapper.Container.GetInstance<IService>();
I get
The IService is registered as 'WCF Operation' lifestyle, but the instance is requested outside the context of a WCF Operation. which totally makes sense since the test run is not a WCF environment.
Upvotes: 1
Views: 517
Reputation: 172826
The whole ide of the DefaultScopedLifestyle property is making it easier to reuse the configuration within different contexts, such as unit testing.
What you should do is supply the scoped lifestyle to a CreateContainer
method. This way both the WCF startup code and the test code can use their own scoped lifestyle.
For testing, the most convenient lifestyle is probably the LifetimeScopeLifestyle. You can wrap a call to GetInstance with a using block for container.BeginLifetimeScope()
.
Do note that the Verify
method internally starts its own scope, and this method will do a lot of testing for you. It checks whether all registrations can be constructed and checks common configuration errors that are really hard to find yourself.
Upvotes: 2