C# 如何让Pex使用DateTime.Now或File.Exists处理函数
我是Pex新手,不知道如何将它用于特定于机器的例程,如DateTime.Now和File.Exists() 我有一个显示截止日期时间和时区偏移的函数C# 如何让Pex使用DateTime.Now或File.Exists处理函数,c#,datetime,pex,C#,Datetime,Pex,我是Pex新手,不知道如何将它用于特定于机器的例程,如DateTime.Now和File.Exists() 我有一个显示截止日期时间和时区偏移的函数 public class CommonDateTime { public static string ConvertToLongStringWithGmtOffset(DateTime cutoffData) { return String.Format( "{0} {1} GMT (loca
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参数化测试,它由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;
}
}
[PexClass(typeof(CommonDateTime))]
[测试夹具]
公共部分类CommonDateTime\u测试
{
///带有GMTOFFSET(日期时间)的敞篷车纵梁测试桩
[PexMethod]
公共字符串转换为带有GMTOFFSET的LongStrings(日期时间\u截止数据)
{
字符串结果=CommonDateTime.ConvertToLongStringWithGmtOffset(\u截止数据);
返回结果;
}
}
但是,这会生成一个特定于机器的测试-当机器处于非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);
}
}
公共部分类CommonDateTime\u测试
{
[测试]
[PexGeneratedBy(类型(CommonDateTime_测试))]
带有GMTOFFSET156()的公共无效转换字符串
{
字符串s;
s=此.ConvertToLongStringWithGmtOffset(默认值(日期时间));
佩克萨塞特·阿雷埃普
(“格林尼治标准时间0001年1月1日星期一上午12:00(当地时间+00:00)”,s);
}
}
在这种情况下我能做什么?我可以告诉它跳过对诸如DateTime.Now或File.Exists()等函数的探索吗。或者我可以告诉它总是使用特定的时区吗?这就是这个项目的目的。它可以让你模拟任何东西,包括内置的静态函数,比如DateTime.Now
适当的“Moled”代码如下所示:
[PexMethod]
public string ConvertToLongStringWithGmtOffset(DateTime _cutOffData)
{
MDateTime.NowGet = () => /* some value */;
string result = CommonDateTime.ConvertToLongStringWithGmtOffset(_cutOffData);
return result;
}
它实际上使用了DateTime.Now
作为示例