Microsoft moles dynamic tool

Moths 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 the {project name} .moles file: point assembly to mole. e.g. <Moles xmlns="http://schemas.microsoft.com/moles/2010/"> <Assembly Name="Samples.Moles"/> </Moles>
  • Build and enable a link to MolesAssemblies / {project_name} .Moles.dll
  • Use the M {class_name} automatically generated role classes.

I noticed that using dynamic assembly does not require the test project to declare the attributes "moled assemblies". This reduces overhead, and the developer only needs to decorate each test method with a host type for moles; but further testing does not need to track which types of tools.

Looking at the automatically generated code (using a disassembler) in molesassemblies, it’s easy to find the required attributes of the toolkit. However, trying to write your own β€œmole assembly”, essentially replacing the auto-generated one, does not work, and the runtime complains that my type must be instrumental. I'm curious what I'm missing.

I noticed that the auto-generated horn code declared the necessary attributes of MoledAssembly. But in my tests, the test project seems to have to declare this property; it cannot be declared a reference to a project. However, in the case of an auto-generated assembly, the APPEARS attribute can be declared "outside". This my assumption is based on what I see when disassembling dlls with auto-generated moles; I can not find any other difference. However, as I try to explain, copying all the code (and attributes) from the disassembled DLL with the generated parent moles and building my own referenced assembly fail at runtime, stating that I did not mark the required assembly test for the tool (i.e. marked using the MoledAssembly module), it is just in my linked assembly.

- update

At this point (probably due to my misunderstanding that my code is missing), I feel that we need to be very specific as to what the assembly has. Let's say we have 4 dlls:

  • Test.dll: the most mestest project. Not declared by MoledAssembly .
  • Moles.dll: created dll auto-generated by using the *.moles file in your project. References 4th dll, (see # 4) Sealed . Declares [assembly: MoledAssembly("Sealed")] . Please note that I am trying to perform a routine injection of moles without this DLL - this is only a conceptual link or its use in our discussion or troubleshooting.
  • MyMoles.dll: My original compiled version of the autogenerated Moles.dll .
  • Sealed.dll: contains the code under the test.

In the answers / comments / questions - give a link to each part in accordance with this list.

+6
source share
1 answer

An assembly attribute is required when using moraine assemblies that are not automatically generated. Moles Tools for Visual Studio automatically alerts the compiler to the availability of generated assemblies if necessary.

When adding a Moles assembly through Visual Studio, the mole assembly is not created until the project is created. In addition, it is not possible to add an assembly attribute for an assembly that does not yet exist. This will cause the compiler to crash. Therefore, Moles also needs to dynamically add commands to the compiler command line, generate assembled assemblies, and then correctly refer to them from the project.

When using a manually created assembly with models, you must enable the assembly attribute, since Moles tools are not aware of its presence due to the fact that the assembly is not autogenerated. The programmer must do the job for Moles.

If you want to go this far, you can use code generation before the compiler takes care. PERL can easily insert the required assembly attribute, if necessary. When the compiler receives the code, it will already have the attribute entered.

An experiment to answer the question:

I was able to reproduce your problem. I was also able to solve the problem by adding an assembly attribute, below the using statement block. I followed these steps to create my sample application:

  • A project has been created for the .NET 4.0 C # class library named ClassLibrary2.
  • Created the following method in Class1:

    public line TestString () {return "Original value."; }

  • Created a test project (TestProject1), right-clicking on the declaration of the TestString method, and then selecting Create Unit Tests ... (Lazy, I know.)

  • Removed excess shit from Class1Test.cs, leaving TestStringTest ().
  • Added mole assembly for mscorlib. (Another lazy shortcut and an extra step in retrospect. I mark this here because this is what I did.)
  • Added mole assembly for ClassLibrary2.
  • Compiled solution using the profile of any profile by default.
  • Used by Redgate (sorry @payo) to decompile ClassLibrary2.Moles
  • A new class library project called MoleClassLibrary has been added.
  • I copied the decompiled code MClass1 and SClass1 to MoleClassLibrary.
  • Removed Class1.moles file and assemblies from TestProject1.
  • Removed (unnecessary) file and mscorlib.moles assemblies from TestProject1.
  • Added MoleClassLibrary reference to TestProject1.
  • The using statement in Class1Test.cs has been updated.
  • Build a solution.
  • Done by TestStringTest () using the Visual Studio 2010 Test View window.
  • The test failed, creating the details:

Test method TestProject1.Class1Test.TestStringTest threw an exception: Microsoft.Moles.Framework.Moles.MoleNotInstrumentedException: System.String ClassLibrary1.Class1.TestString () was not a tool . To resolve this issue, add the following attribute to the test project:

using Microsoft.Moles.Framework; [Build: MoledAssembly (TypeOf (ClassLibrary1.Class1))]

I added the recommended assembly attribute to the file. After that, the testing method passed successfully. I suspect that the compiler automatically references the generated assembly builds, eliminating the need for an assembly attribute. I tried to copy the MoleClassLibrary binaries to the MolesAssemblies directory and create a MoleClassLibrary.moles file to test this theory. The test passed only when I turned on the assembly attribute. This result is inconclusive for my hypothesis.

Here is the code for Class1Test.cs:

 using ClassLibrary1; using Microsoft.Moles.Framework; using Microsoft.VisualStudio.TestTools.UnitTesting; using MoleClassLibrary; [assembly: MoledAssembly(typeof(ClassLibrary1.Class1))] namespace TestProject1 { [TestClass()] public class Class1Test { [TestMethod()] [HostType("Moles")] public void TestStringTest() { var target = new Class1(); var expected = "Mole value."; string actual; MClass1.AllInstances.TestString = value => expected; actual = target.TestString(); Assert.AreEqual(expected, actual); } } } 
+4
source

Source: https://habr.com/ru/post/890001/


All Articles