How to use mole manually in Visual Studio Express?

2019-09-08 09:47发布

问题:

I searched for tags "Moles" and "Visual Studio Express", and found no questions. That probably means I'm the only person trying to use Express with Moles :)

In Express, there are no options such as "Add moles assembly", or "Add mscorlib".

I've managed to get the tutorial for the DateTime Y2Kbug working after manually adding a couple of references that would normally be handled by VS. So, that means that I can mole any of the pre-moled stuff in System or mscorlib, which will be useful.

My problem is that I cannot seem to mole any of my own classes or methods in the SUT. I don't believe that the compiler sees any of the .moles file that contains the assemblies to mole.

I proved (?) this by attempting to exclude DateTime from the Y2kbug tutorial, using:

<MoleGeneration>
    <Types>
        <Remove FullName="DateTime!" />
    </Types>
</MoleGeneration>

But the "bug" was asserted as usual :)

Is it possible to generate non-System moles as I need them by using something I saw in this forum: stackoverflow.com/questions/6272217/microsoft-moles-dynamically-instrument

Call Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, object _receiver, MethodInfo method);

If I use the "SetMole" method, does that mean that I don't need "assembly" files which are not seen anyway? If so, would it be possible to give some examples of the use of "SetMole". (I'm not a techie.)

As suggested by Mike Christian, I used moles.exe OrigValueP.dll. This created OrigValueP.Moles.dll in the MolesAssemblies subfolder.

Add OrigValueP.Moles.dll as a reference to the test project.

View OrigValueP.Moles in the Object Browser. I now see MOrigValue.AllInstances. So that's a huge step forward.

However, I don't see any Moles equivalent of my method:

public string TestString() { return "Original value"; }

[Deleted lines relating to the command, "moles.exe OrigValueP.Moles.dll", because MC suggested I'm "attempting to mole the mole assembly".]

So, I have one question (which I will continue to try to answer myself): Why was there no Moled method for TestString()?

回答1:

The Moles documentation states Moles is compatible with Visual Studio 2008 Professional or Visual Studio 2010 Professional or better. http://research.microsoft.com/en-us/projects/pex/molestutorial.docx

Don't forget that executing the moles compiler via command line requires that you perform compilation in the following order: 1. Target assemblies 2. Moles (against the target assemblies) 3. Test assemblies

The Moles Manual provides the following command line instruction:

http://research.microsoft.com/en-us/projects/pex/molesmanual.docx

Code Generation Command Line The Moles framework includes a command-line application that can generate the code by following the instructions in .moles files or directly from assemblies. By default, the command-line tool compiles the stub types and mole types into an assembly and places it into a MolesAssemblies subfolder. The following list shows examples of typical usage.

Generate mole types and stub types for a particular assembly moles.exe assembly.dll Generate mole types and stub types from an existing .moles file moles.exe assembly.moles Generate mole types and stub types under a particular namespace moles.exe assembly .dll /nf:MyNamespace Get detailed help about usage of moles.exe moles.exe help



回答2:

[This whole answer has been appended to the original question. Sorry for all the mess.]