I was trying to find a consistent description on how to use moles isolation framework but haven't found much on this topic. So far i did the following:
- Download moles from here (x86 version).
- Install it.
- Here guy describes how to use it with custom library. So i added moles assembly for my own library. After rebuild the assembly appeared in references.
- Then i tried to add using of .Moles namespace and build the project but it failed with bunch of errors. Example with MDateTime didn't work either. MDateTime just didn't have any method.
- Considering that was 5th failed attempt to get it work i uninstalled it from the system.
So the question: does moles work for anybody at all? If yes how did you get there? Should i also install pex to make it work? What if i want to use it on build server from msbuild script? I saw they mentioned support of msbuild, but has anyone real experience with using it from msbuild?
For me it looks very raw to be used in serious development process. Maybe someone has another opinion?
Thanks.
Yes, it works fine. I assume you're installing v0.94.51023.0. For Moles to function properly, you must do a few things, to set up the test class. These quickly become second nature -- don't worry!
You must create a "moled assembly", for each assembly against which you wish to use Moles:
a. Right-click an assembly in the "References" node of the Solution Explorer, and then select "Add Moles Assembly". b. If you wish to mole a .NET Framework assembly, right-click the References node, and then select "Add Moles Assembly for mscorlib'".
Reference the Microsoft.Moles.Framework assembly in the .CS file:
using Microsoft.Moles.Framework;
Build the project. This causes the appropriate Moles framework references to be added to the project. As @Lara commented, step 3 will fail, without adding the references.
Reference moled assemblies in the .CS file:
using MyNamespace.Moles;
Decorate test methods that use Moled types with the [HostType("Moles"] attribute
[HostType("Moles")]
Identify either an entire assembly or individual types that are used in the .CS file, by using the assembly attributes:
using MyNameSpace.MyAssemblyName.Moles;
[assembly: MoledAssemblyType("MyNameSpace.MyAssemblyName")]
[assembly: MoledType(typeof(MyNameSpace.MyAssemblyName.MyClass))]
When the test project is compiled, Moles copies the "moled" assemblies, and then injects detours into the copy. The namespace of moled assemblies and types are appended with ".Moles". Therefore, "MyNameSpace.MyAssembly" becomes "MyNameSpace.MyAssembly.Moles". I'm sure you are also familiar with the "S" and "M" prefixes to the type names of moled assemblies. If not, please refer to the Moles Reference Manual.
It works quite fine for me, and I just tried it out with MDateTime as well. Just make sure that you either put the correct namespace in your using or use System.Moles.MDateTime directly in your code. Intellisense should then give you all methods and properties to set it up as desired.
As you've already said that you rebuilt your project after adding the moles assembly, this shouldn't be the problem anymore.
For some classes (I'm not quite sure right now, maybe those that are not part of mscorlib?) I also had to add the
[assembly: MoledType( typeof( HttpContext ) )]
to the top of my unit test class (above the namespace), and also
[HostType( "Moles" )]
as attribute to my test methods, but this is not necessary for MDateTime.
Did you download the most recent version and also update your VS2010 to the latest SP?
Hope some of this helps...
G.
来源:https://stackoverflow.com/questions/6637866/has-anyone-made-moles-work-properly