When writing a unit test, you want to hold the code under test in a vice. You want the code under test to collaborate with objects whose behavior you control so that you can inject controlled conditions into the code under test. This is fairly straightforward when the code under test collaborates with a class that implements an interface. You can simply write a test harness class with the desired behavior. The test harness class is usually called a “fake” object or a “mock” object. It is not the real collaborator, but an object whose purpose is to inject the desired conditions into the code under test to validate its behavior under different circumstances.
However, Win32 code has another sort of collaborator that is more difficult to fake — the global functions in the Win32 API. What happens when your code calls LoadLibrary
and the file can’t be found? Furthermore, with a true unit test you don’t even want to do file I/O because that will make the test run slow. You want unit tests to run as fast as possible because you will be running them often as you make changes to the production code.
Read the rest of this entry »