如何让 Pex 使用 DateTime.Now 或 File.Exists 处理函数

发布于 2024-12-21 17:19:16 字数 1545 浏览 3 评论 0原文

我是 Pex 的新手,我不知道如何将它与机器特定的例程(如 DateTime.Now 和 File.Exists())一起使用。

我有一个用于显示带有时区偏移的截止日期时间的函数。

public class CommonDateTime
{
    public static string ConvertToLongStringWithGmtOffset(DateTime cutoffData)
    {
        return String.Format(
            "{0} {1} GMT (local time is {2})", 
            cutoffData.ToLongDateString(), 
            cutoffData.ToShortTimeString(), 
            DateTime.Now.ToString("zzz"), // here is the problem...
            CultureInfo.InvariantCulture);
    }
}

我有一个由 Pex Explorer 生成的 Pex 参数化测试,

[PexClass(typeof(CommonDateTime))]
[TestFixture]
public partial class CommonDateTime_Test
{
    /// <summary>Test stub for ConvertToLongStringWithGmtOffset(DateTime)</summary>
    [PexMethod]
    public string ConvertToLongStringWithGmtOffset(DateTime _cutOffData)
    {
        string result = CommonDateTime.ConvertToLongStringWithGmtOffset(_cutOffData);
        return result;
    }
}

但这会生成一个特定于机器的测试 - 当机器位于非 GMT 时区时,它会失败。

public partial class CommonDateTime_Test
{
    [Test]
    [PexGeneratedBy(typeof(CommonDateTime_Test))]
    public void ConvertToLongStringWithGmtOffset156()
    {
        string s;
        s = this.ConvertToLongStringWithGmtOffset(default(DateTime));
        PexAssert.AreEqual<string>
            ("Monday, January 01, 0001 12:00 AM GMT (local time is +00:00)", s);
    }
}

在这种情况下我能做什么?我可以告诉它跳过探索引用 DateTime.Now 或 File.Exists() 等函数的函数吗?或者我可以告诉它始终使用特定的时区吗?

I'm new to Pex and I can't see how to use it with machine specific routines like DateTime.Now and File.Exists().

I have a function for displaying a cut-off DateTime with the time zone offset.

public class CommonDateTime
{
    public static string ConvertToLongStringWithGmtOffset(DateTime cutoffData)
    {
        return String.Format(
            "{0} {1} GMT (local time is {2})", 
            cutoffData.ToLongDateString(), 
            cutoffData.ToShortTimeString(), 
            DateTime.Now.ToString("zzz"), // here is the problem...
            CultureInfo.InvariantCulture);
    }
}

I have a Pex parameterized test which gets generated by the Pex Explorer

[PexClass(typeof(CommonDateTime))]
[TestFixture]
public partial class CommonDateTime_Test
{
    /// <summary>Test stub for ConvertToLongStringWithGmtOffset(DateTime)</summary>
    [PexMethod]
    public string ConvertToLongStringWithGmtOffset(DateTime _cutOffData)
    {
        string result = CommonDateTime.ConvertToLongStringWithGmtOffset(_cutOffData);
        return result;
    }
}

However this generates a test which is machine-specific - it fails when the machine is in a non-GMT timezone.

public partial class CommonDateTime_Test
{
    [Test]
    [PexGeneratedBy(typeof(CommonDateTime_Test))]
    public void ConvertToLongStringWithGmtOffset156()
    {
        string s;
        s = this.ConvertToLongStringWithGmtOffset(default(DateTime));
        PexAssert.AreEqual<string>
            ("Monday, January 01, 0001 12:00 AM GMT (local time is +00:00)", s);
    }
}

What can I do in this situation? Can I tell it to skip exploring functions which refer to functions such as DateTime.Now or File.Exists(). Or can I tell it to always use a specific time zone somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你列表最软的妹 2024-12-28 17:19:16

这就是 Moles 项目的用途。它可以让您模拟几乎所有内容,包括内置静态函数,例如 DateTime.Now

适当的“Moled”代码如下所示:

[PexMethod]
public string ConvertToLongStringWithGmtOffset(DateTime _cutOffData)
{
    MDateTime.NowGet = () => /* some value */;

    string result = CommonDateTime.ConvertToLongStringWithGmtOffset(_cutOffData);
    return result;
}

这是一个较长的教程,它实际上使用 DateTime.Now 作为示例。

This is what the Moles project is for. It lets you mock basically anything, including built-in static functions like DateTime.Now.

The appropriate 'Moled' code would look something like this:

[PexMethod]
public string ConvertToLongStringWithGmtOffset(DateTime _cutOffData)
{
    MDateTime.NowGet = () => /* some value */;

    string result = CommonDateTime.ConvertToLongStringWithGmtOffset(_cutOffData);
    return result;
}

Here's a longer tutorial which actually uses DateTime.Now as its example.

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