The following things must be done, to use Moles in a test. This looks like a lot, now. However, you'll quickly get the hang of things, and have these steps done in seconds! Using my CodeRush Moles templates will help speed things up.
- Add a mole assembly for the assemblies requring call detours
- Add a mole assembly for mscorlib, if needed (see How Do I Mole .NET Framework Types?)
- Compile the solution, to generate the mole assemblies
- Add using declarations:
using Microsoft.VisualStudio.TestTools.UnitTesting;(Pretty much standard)
- using Microsoft.Moles.Framework; (Moles framework support)
- using YourProjectNamespace.Moles; (The moled assembly for your project -- replace YourProjectNamespace with your project's namespace)
- Add any moled .NET framework assembly using declarations, ending in .Moles.
- For each moled .NET type, add a MoledType assembly attribute, immediately after the using declarations. Failure to add requisite attributes causes the compiler to display an error, which provides guidance on adding the attribute. Check out my CodeRush Templates for Moles, to quickly insert these attributes!
[assembly: MoledAssembly(typeof("System.IO"))] [assembly: MoledType(typeof(System.IO.File))]
NOTE: Avoid using MolesAssembly, unless you are using the majority of the moled types in the assembly. The Moles Framework will only prime the types identified by MoledType, but primes all types of the assembly, when using MoledAssembly.
- All test methods that call one or more moled types must be decorated with the HostType attribute:
NOTE: The HostType attribute must be added to test methods that call another method containing calls to moled types! This is true, even when the test method does not directly contain calls to mole types.