如何让 Pex 使用 DateTime.Now 或 File.Exists 处理函数
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 Moles 项目的用途。它可以让您模拟几乎所有内容,包括内置静态函数,例如
DateTime.Now
。适当的“Moled”代码如下所示:
这是一个较长的教程,它实际上使用
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:
Here's a longer tutorial which actually uses
DateTime.Now
as its example.