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); } } }