Moles can be used in two ways:
Manually
- Including [assembly: MoledType(typeof(_type_to_instrument))]
- Specify [HostType("Moles")]
- Call Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, object _receiver, MethodInfo method);
Dynamically
- Add a {project name}.moles file: specifying assembly to mole. e.g.
<Moles xmlns="http://schemas.microsoft.com/moles/2010/"> <Assembly Name="Samples.Moles"/> </Moles>
- Build and include reference to MolesAssemblies/{project_name}.Moles.dll
- Use M{class_name} auto-generated mole classes.
What I noticed is that using the dynamic assembly doesn't require the test project to declare 'moled assemblies' attributes. This reduces the overhead, and a developer need only decorate each test method with the host type for moles; but further testing doesn't need to keep track of what types to instrument.
Looking at the auto-generated code (using a disassembler) in the molesassemblies, it is easy to find that the needed instrumentation attributes. However, trying to write my own 'mole assembly', essentially replacing the autogenerated one, doesn't work and the runtime complains that my type needs to be instrumented. I am curious what I'm missing.
I noticed that the autogenerated moles code declared the necessary MoledAssembly attributes. But in my tests, the test project seems to have to declare this property; it can't be declared by a referenced assembly to the project. However, in the case with the autogenerated assembly, it APPEARS the attribute can be declared "outside". This is my assumption based what I can see with disassembling the autogenerated moles dll; I cannot find any other difference. However, as I'm trying to explain, copying all the code (and attributes) from the disassembled autogenerated moles dll and building my own referenced assembly fails at runtime saying I have not marked the needed assembly-in-test to be instrumented (i.e. marked with the MoledAssembly) - tho it has, simply in my referenced asssembly.
-- update
At this point (likely due to my misunderstanding of what my code is missing) I feel we need to be very specific about what assembly has what. Let's say we have 4 dlls:
- Test.dll: The mstest project. Does not declared
MoledAssembly
. - Moles.dll: The autogenerated dll created when using a
*.moles
file in your project. References the 4th dll, (see #4)Sealed
. Declares[assembly: MoledAssembly("Sealed")]
. Note that I'm trying to accomplish manual mole injection without this dll - it's only a conceptual reference or to be used in our discussion or troubleshooting. - MyMoles.dll: My from-source compiled version of the autogenerated
Moles.dll
. - Sealed.dll: Contains the code-under-test.
In answers/comments/questions - let's refer to each part according to this list.
var type = typeof(M___)
where the M___ type is in the Moles.dll. This leads to be believe there is some manifest rule that is executing. I need to find this manifest. – Umbelliferous