One small problem I encountered when getting into Dependency Injection (DI) and Inversion of Control (IoC) was that even though all my services were now beautifully SOLID and test-driven, quite often it was all wasted because I forgot to register them in my IoC container, causing massive errors that wouldn’t be detected until runtime (oops).
Luckily, it is very easy to write a test to check everything has been registered properly. All you need is a bit of reflection to find all the interfaces in an assembly, then try to resolve them all:
[Test] public void Should_be_able_to_resolve_all_interfaces_in_domain() { var domain = Assembly.GetAssembly(typeof(Book)); var interfaces = domain.GetTypes().Where(t => t.IsInterface); foreach (Type @interface in interfaces) { Assert.IsNotNull(ServiceLocator.Current.GetInstance(@interface)); } }
We can make this even nicer with NUnit 2.5. Instead of a for loop with multiple asserts within one test case (hmm) that can only show one failure at a time (ugh), we can use NUnit parameterized tests to automagically generate a separate test case for each interface we need to resolve:
That’s heaps easier to read (otherwise Unity’s exceptions are very terse), and we can see multiple failures in one run. To make NUnit generate this without typing them all out by hand, all you need is the new ValueSource attribute, which lets you choose a method, field or property or method that returns an IEnumerable set of objects:
public IEnumerable<Type> GetDomainInterfaces() { var domain = Assembly.GetAssembly(typeof(Book)); return domain.GetTypes().Where(t => t.IsInterface); } [Test] public void Should_be_able_to_resolve_domain_service( [ValueSource("GetDomainInterfaces")]Type @interface) { Assert.IsNotNull(ServiceLocator.Current.GetInstance(@interface)); }
Note I include this with my integration tests, because it can take a few secs to run (e.g. I keep my NHibernate ISession in the container, and building the session factory takes a long time).