Okay so I have been trying to get into IoC lately. However, I keep running into one hurdle – that is the fact that I love using mock objects.
They are quick and painless to setup.
However, if I use IoC all over the place in my code then it forces me to create test implementations (and configurations) of my objects instead of using mock objects (ie. using moq).
The end result is that I end up with enormous configuration files for testing.
In addition there are many scenarios in testing where I require different behaviors out of my classes on a test-to-test basis. With moq objects this is extremely easy. How would you do something similar with IoC?
Any help would be much appreciated.
Thanks,
Mike
IoC should make using mock objects easier, not harder.
Several IoC Container frameworks will allow you to define pre-existing objects to inject; with Moq you’d just set it up for myMockObject.Object.
EDIT: Example of configuring Unity with a mock:
As an alternative, you can just pass the mock object into the constructor of the class under test (for constructor injection) and bypass the IoC container entirely in your unit tests.
EDIT: Josh’s answer is a good example of the alternative. I would generally go with his solution rather than reconfiguring the container.