Has anyone made moles work properly?

我与影子孤独终老i 提交于 2019-12-07 22:30:28

问题


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:

  1. Download moles from here (x86 version).
  2. Install it.
  3. 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.
  4. 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.
  5. 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.


回答1:


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.




回答2:


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

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