Update 29/04/10:
In contrary to what I initially stated in this post, Moles is not only available as part of the Pex framework, but also stand-alone on Visual Studio Gallery, and this is completely free (no MSDN subscription required). - Thanks to Peli for the correction...
Usual opensource mocking frameworks (like e.g. Moq or ) can mock only interfaces and virtual methods. In contrary to that, Microsoft’s Moles framework can ‘mock’ virtually anything, in that it uses runtime instrumentation to inject callbacks in the method MSIL bodies of the moled methods. Therefore, it is possible to detour any.NET method, including non-virtual/static methods in sealed types. This can be extremely helpful when dealing e.g. with code that calls into the.NET framework, some third-party or legacy stuff etc…
Some useful collected resources (links to website, documentation material and some videos) can be found in my under this link:
A Gallio extension for Moles
Originally, Moles is a part of Microsoft’s Pex framework and thus integrates best with Visual Studio Unit Tests (MSTest). However, the Moles sample download contains some additional assemblies to also support other unit test frameworks. They provide a Moled attribute to ease the usage of mole types with the respective framework (there are extensions for NUnit, and MbUnit v2 included with the samples). As there is no such extension for the Gallio platform, I did the few required lines myself – the resulting Gallio.Moles.dll is included with the.
With this little assembly in place, it is possible to use Moles with Gallio like that:
[Test, Moled]
public void SomeTest
{
...
What you can do with it
Moles can be very helpful, if you need to ‘mock’ something other than a virtual or interface-implementing method. This might be the case when dealing with some third-party component, legacy code, or if you want to ‘mock’ the.NET framework itself.
Generally, you need to announce each moled type that you want to use in a test with the MoledType attribute on assembly level. For example:
[assembly: MoledType(typeof(System.IO.File))]
Below are some typical use cases for Moles. For a more detailed overview (incl. naming conventions and an instruction on how to create the required moles assemblies), please refer to the reference material above.
