Has anyone made moles work properly?

℡╲_俬逩灬. 提交于 2019-12-06 11:40:09

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!

  1. 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'".

  2. Reference the Microsoft.Moles.Framework assembly in the .CS file:

    using Microsoft.Moles.Framework;

  3. 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.

  4. Reference moled assemblies in the .CS file:

    using MyNamespace.Moles;

  5. Decorate test methods that use Moled types with the [HostType("Moles"] attribute

    [HostType("Moles")]

  6. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!