Even More Independence
In my post last week I discussed a solution for having control over the class being created by the class being tested. There is another way of doing this that is worth mentioning: creating a Factory class. Let's assume we created an interface (IBar) that our Bar class implements. Now we need to create a class (BarFactory) that contains methods for creating instances of Bar. I am inclined to take this one step further and create an interface for the factory class (IBarFactory which will create instances of IBar.) Now we can pass an instance of IBarFactory to Foo in the constructor. We can mock IBarFactory and ensure that we are only testing Foo.