将 Moles 与 DateTime 结合使用
我开始在单元测试中使用 Moles,并且在文档方面遇到了一些困难。
我想摩尔 DateTime.Now。
如果您查看旧的执行此操作的方法是添加对 mscorlib 的引用,然后为其添加一个存根文件(添加新项 -> 用于测试的存根和摩尔)。
“用于测试的存根和摩尔”模板已被弃用,您所需要做的就是右键单击引用并选择“添加摩尔程序集”,这很好。
VS2010不允许您直接添加对mscorlib的引用,因为我们有对“System”的引用,这没关系,因为我可以在对象浏览器中看到DateTime作为此命名空间的一部分。
如果我为系统引用添加摩尔程序集并重建,我仍然无法解析 MDateTime。
有什么建议吗?
I'm starting to using Moles in unit tests and am struggling a little with documentation.
I want to mole DateTime.Now.
If you look around the old way of doing this was to add a reference to mscorlib, then add a stubx file for it (Add New Item -> Stubs And Moles For Testing).
The 'Stubs and Moles for Testing' template has been deprecated, instead all you need do is to right click a reference and select 'Add moles assembly', whch is fine.
VS2010 does not allow you to add a reference directly to mscorlib, because we have a reference to "System", this is ok as I can see DateTime in object browser as part of this namespace.
If I add a moles assembly for the System reference and rebuild I still can't resolve an MDateTime.
Any suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于mscorlib的Moles,您需要直接右键单击您的测试项目的References。您将拥有为 mscorlib 添加 Moles 组件。
然后,将
using System.Moles;
添加到您的测试类,因为您需要 System.DateTime 的 Moles(实际上,您还需要更多)。如果您运行此测试,它将失败,因为您需要添加:
然后,您的测试将会成功。
不要忘记 Moles 不能与某些特殊类型的 mscorlib 一起使用。
For Moles of mscorlib, you need to right-click directly on the References of your test project. You will have Add Moles Assembly for mscorlib.
Then, add
using System.Moles;
to your test class because you want Moles of System.DateTime (actually, you need a little more).If you run this test, it will fail because you need to add:
Then, your test will succeed.
Don't forget that Moles cannot be used with some special types of mscorlib.