摩尔和重构代码
我们正在我的公司着手一个新的绿地项目,并决定我们将使用 Microsoft 的 Moles 框架来进行单元测试中的所有模拟。理想情况下,我的经理不想引入任何其他 Mocking 框架(例如 NSubstitute 或 Moq)来使项目复杂化。然而我发现,由于 Moles 生成模拟和存根的性质,它不能很好地与重构工具配合使用。
例如,我们使用 resharper,如果我有以下接口,moles 将生成如下存根:
IMyInterface -> SIMyInterface
现在,如果我将 IMyInterface 重构为另一个名称,例如:IMyNewInterface,
那么在我的单元测试中,Stub 类显然不会被重构,因为它从根本上具有不同的名称。
我可以看到,一旦我们得到许多单元测试的排列,这是一个大问题,重构将成为一场噩梦,而口头禅将是“不要改变任何东西!”
有谁有类似的经验或知道可以处理 Moles 的重构工具吗?
谢谢梅林和迈克。我的团队决定妥协并仅将 Moles 用于我们无法使用标准工具模拟的类型,然后使用另一个框架(例如 NSubstitute)来处理其他所有内容。
We are embarking on a new greenfield project in my company and it has been decided that we will use Microsoft's Moles framework for ALL our mocking in unit tests. My manager ideally does not want to introduce any other Mocking frameworks such as NSubstitute or Moq to complicated the project. However I have found that due to the nature in which Moles generates Mocks and Stubs that it does not work nicely with refactoring tools.
For example, we use resharper and if I had the following interface, moles will generate a stub like so:
IMyInterface -> SIMyInterface
Now if I were to refactor IMyInterface to another name such as: IMyNewInterface
then in my unit tests the Stub class is obviously NOT be refactored due to it fundamentally having a different name.
I can see this is a big problem once we get many permutations of unit tests, that refactoring will become a nightmare, and the mantra will be "Just don't change anything!"
Does anyone have similar experiences or know of a refactoring tool that can handle Moles?
Thanks both Merlyn and Mike. My team has decided to compromise and use Moles only for types that we can't mock with standard tools, and then use another framework such as NSubstitute for everything else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2012 年 8 月 16 日更新 - 请使用假货而不是 Moles。 Fakes 随 Visual Studio 2012 一起提供,并且是 Moles 完全支持的产品版本。
Moles 没有第 3 方供应商支持;因为,它不是正式发布和支持的技术,并且仍在变化。我创建了一些 CodeRush 模板,可以生成 Moles 和 Pex 属性以及测试方法;但是,这对重构类型名称没有帮助。
我同意,您在代码和测试程序集中重构名称的想法是一个很好的想法。讽刺的是,摩尔类型无法重构,因为它们来自已编译的程序集。您能做的最好的事情就是重构代码,然后在测试项目中执行搜索和替换(恶心)。是的,这很丑陋,尤其是在重构了几件事之后。
MOLES 建议:
我建议 RiSE 调整 Moles(再次)以生成未编译的 Moles 项目,而不是生成已编译的程序集。我知道组装事情要容易得多,但我预见到这将允许开发人员重构摩尔类中的摩尔和存根类型名称。此外,还有重构工具,例如 Refactor! Pro 应该能够跨代码、摩尔和测试项目自动重构代码和摩尔类型名称。
UPDATE 16 AUG 2012 - Please use Fakes instead of Moles. Fakes are shipped with Visual Studio 2012, and is the fully-supported product release of Moles.
Moles has no 3rd party vendor support; because, it is not an officially released and supported technology, and is still changing. I have created some CodeRush templates that produce Moles and Pex attributes and test methods; however, this doesn't help with refactoring type names.
I agree, your idea to refactor names across the code and test assemblies is a good one. Ironically, the mole types are unable to be refactored, as they come from a compiled assembly. The best you can do is refactor the code, and then perform a search and replace in the test project (yuck.) Yes, this is ugly, especially after refactoring several things.
MOLES SUGGESTION:
I suggest RiSE tweak moles (yet again) to produce a Moles project that is not compiled, rather than generating a compiled assembly. I know the assembly thing is much easier, but I foresee a tremendous This allows the developer to refactor mole and stub type names in the mole classes. Furthermore, refactoring tools, such as Refactor! Pro should be able to automatically refactor both the code and mole type names, across the code, mole, and test projects.
根据 Microsoft Moles 参考手册,在“代码生成配置”下”,第 11 页:
看来禁用所述编译的方法是添加一个
Compilation
元素作为(根)Moles
元素的子元素(如果不存在),并向所述子元素添加 aProjectTemplate
属性,将其值设置为 .csproj 文件的路径。这是基于我对 Visual Studio 中的 XML 编辑器显示的所述属性的 IntelliSense 描述的阅读(取自 Moles 架构中的文档,该架构在我的计算机上位于C:\Program Files\Microsoft Visual Studio 10.0\Xml \Schemas\moles.xsd
),内容如下:According to the Microsoft Moles Reference Manual, under “Code Generation Configuration”, on page 11:
It appears the way to disable said compilation is to add a
Compilation
element as a child of the (root)Moles
element if none is present, and add to said child element aProjectTemplate
attribute, setting its value to the path for a .csproj file. This is based on my reading of said attribute’s IntelliSense description displayed by the XML Editor in Visual Studio (taken from the documentation in the Moles schema, which on my computer is atC:\Program Files\Microsoft Visual Studio 10.0\Xml\Schemas\moles.xsd
), which reads as follows: