The Extract and Override pattern
I'm in the process of reading Roy Osherove's book, "The Art of Unit Testing". It's a great book - recommended for both Gurus and N00bs; I've learnt a couple of new things.
One of those ideas is the "Extract and Override" pattern. It is a dependency breaking technique described in "The Art of Unit Testing", but I think the name stems from Michael Feather's book, "Working Effectively with Legacy Code".
I'll describe the scenario where this pattern can help. Imagine you need to unit test a class, BookingManager. You specifically want to test the MakeBooking method. Note how the MakeBooking method depends on other methods and those methods use external dependencies:
What the Extract and Override pattern allows is for you to test the MakeBooking method in isolation, without the all the logic and external dependencies in the other methods it depends on. You extract a sub class from the class under test, and *override* the methods you don't care about. Those method can return predefined values. Like so:
We had to mark the methods we are overriding as virtual. The test can create an instance of THIS class instead of the actual class, but it will still test the actual MakeBooking method. The methods our MakeBooking method depends on are stub methods - they simply succeed.
This pattern helps you to isolate the method you want to test from other methods in the same class under test. Pretty useful!
