The Moles Framework
The Moles framework emerged as part of a larger Microsoft Research project named Pex, which aims to provide a systematic, automated approach to unit testing. Pex provides comprehensive test automation based on parameterized unit tests, and is not covered in this brief overview. Moles also includes many features that are beyond the scope of this document. Moles is a Visual Studio Power Tool—it's available as a free download from Visual Studio Gallery, and supports both Visual Studio 2010 and Visual Studio 2008. Pex is available for MSDN subscribers as a subscriber download. For more information about Pex and Moles, see the Pex project site.
The Moles framework actually supports two different kinds of substitution class—stub types and mole types. These two approaches allow you to create substitute classes for code dependencies under different circumstances:
- Stub types provide a lightweight isolation framework that generates fake stub implementations of virtual methods and interfaces for unit testing.
- Mole types use a powerful detouring framework that uses code profiler APIs to intercept calls to dependency classes and redirects the calls to a fake object.
Generally speaking, you should use stubs (or an alternative mocking framework) to create substitute implementations for your own code and for any third-party code that exposes virtual methods and interfaces that you can override. When it is not possible to create stubs for third-party code, such as when the code uses sealed classes or static, non-virtual methods, you should use moles to redirect calls to the code. Later sections provide more guidance on when it is appropriate to use stubs and moles. First, the next sections describe how you can use these two components.