In the context of the Microsoft .Net Framework, I’m really curious about how mocking frameworks (Rhino Mocks, Moq, NMock, etc.) actually create the mock objects from a given type.
I’m interested in either the specific mechanics of one method or, if they use different methods perhaps some overview of the different ways. Alternatively, if anyone could point me at some articles, that’d be great too.
There are different techniques out there for mocking.
Some mocking libaries like Moq and RhinoMocks use Castle Dynamic proxies. Essentially, they use reflection and runtime code generation (CodeDom) to dynamically generate classes that either implement an interface being mocked, or derive from a non-sealed class that’s being mocked.
TypeMock is a bit different – it uses the Profiler APIs to intercept and redirect calls to any method on any type. This makes it capable of mocking sealed types, system classes, and even intercept and divert calls to non-virtual methods of concrete types.
UPDATE: From the TypeMock website: