One thing I like to do when I am using a IoC (Inversion of Control) container is to create simple test that ensure that my objects have been wired up correctly and can be created via the IoC container.
I know this may seem like overkill, but I feel that by having my IoC bindings covered by tests I am covering all my bases.
So, what does testing my bindings do for me
- Allows me to know if my object is configured correctly
By having these tests I will know immediately if I have somehow broken my IoC bindings glue (yes, glue is the technical term in this case).
It is not hard to phantom that when making changes to your IoC bindings logic (either via config files or via inline code) you can mistakenly break something. Having these tests provides me immediate feedback if this were to happen.
- Allows me to know if my objects dependencies are configured correctly
Setting up the wiring for your main object is only half the battle. When using an IoC container many time you are going to have that container do some sort of Constructor Injection of your dependencies.
By having tests that simply create my object with IoC I will find out immediately if one of my dependency objects is not wired up. This scenario is pretty common and having this test can save you some headaches.
Here is the test code I use for my IoC bindings tests:
[Test]
public void CanCreateViaDI()
{
var iocObject = ObjectFactory.GetInstance< YourObjectHere >();
Assert.That( iocObject, Is.Not.Null );
}
Hope this helps someone.
Till next time,
[----- Remember to check out DimeCasts.Net -----]
Posted
09-10-2008 6:53 AM
by
Derik Whittaker