I am considering converting some of our complex creation code to use the IoC container, Autofac, and since I really believe in TDD, I write unit tests for module configuration.
Most functions are very easy to check, for example.
var obj = container.Resolve<IThing>();
Assert.IsInstanceOfType(obj, typeof(ThingImplementer));
But we have a number of cases where we have several executors of the same interface, and different executors are passed to different specific classes. I resolved this using named registration, for example.
builder.RegisterType<ThingImplementer>().Named<IThing>("Implementer1");
builder.RegisterType<OtherImplementer>().Named<IThing>("Implementer2");
builder.Register(c => new Foo(c.ResolveNamed<IThing>("Implementer1"))).As<IFoo>();
What I can't figure out is an easy way to write unit test to ensure that Foo gets a ThingImplementer, not a OtherImplementer. I am wondering if it is worth doing, we have high-level integration tests that cover this, but they do not provide documentation or refactoring the benefits that are performed in unit tests.
Could you write unit test for this? If so, how?
source
share