The projects I work on make use of Unit Testing via the NUnit Framework and the TestDriven Visual Studio addin. However, we haven’t made use of a Mocking Framework and I finally decided to have a look around and a quick experiment.
The basic problem is that you want to test the working of a method/property on a class but you don’t want to let the class call further dependant objects. Why not? The most important reasons for me is that I don’t want to configure a whole system in order to run a simple test.
A number of Mocking Frameworks use the ‘Mock Object Pattern’ that relies upon the dependant service classes always exposing interfaces. I really don’t like this pattern, ok some would argue that it is good OO to always expose an interface, but I don’t like implementing code features for the sake of supporting a framework that isn’t required for the solution. But the real problem is that I now have to pass the interfaced object into the dependant clients…NO!
So I quickly gravitated to TypeMock which uses the CLR Profiler to intercept the calls to the services and inject code to mock the results. It’s very good, however IMO the documentation is surprisingly poor. I recommend reading through the Test Patterns documents but the code snippets do have a few bugs. So I’ve included a VS2005 solution (ConsoleApplicationTypeMock.zip) with this blog entry that implements the first authentication sample. I’ve used it a few times to test my tests when I get bugs with the mocking framework.
Another quick tip, when you do get errors from the Mocking Framework, pay very close attention to the "type" in the message.