将 Moles 与 DateTime 结合使用

发布于 2024-11-06 02:07:27 字数 361 浏览 9 评论 0原文

我开始在单元测试中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俏︾媚 2024-11-13 02:07:27

对于mscorlib的Moles,您需要直接右键单击您的测试项目的References。您将拥有为 mscorlib 添加 Moles 组件
然后,将 using System.Moles; 添加到您的测试类,因为您需要 System.DateTime 的 Moles(实际上,您还需要更多)。

[TestMethod()]
[HostType("Moles")]
public void DateTimeMolesTest()
{
    DateTime date = new System.DateTime(2000, 1, 1, 2, 3, 4, 5);
    MDateTime.NowGet = () => date;
    Assert.AreEqual(date, DateTime.Now);
}

如果您运行此测试,它将失败,因为您需要添加:

using Microsoft.Moles.Framework;
[assembly: MoledType(typeof(System.DateTime))]

然后,您的测试将会成功。
不要忘记 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).

[TestMethod()]
[HostType("Moles")]
public void DateTimeMolesTest()
{
    DateTime date = new System.DateTime(2000, 1, 1, 2, 3, 4, 5);
    MDateTime.NowGet = () => date;
    Assert.AreEqual(date, DateTime.Now);
}

If you run this test, it will fail because you need to add:

using Microsoft.Moles.Framework;
[assembly: MoledType(typeof(System.DateTime))]

Then, your test will succeed.
Don't forget that Moles cannot be used with some special types of mscorlib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文