Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeMock到伪DateTime.Parse_Datetime_Typemock - Fatal编程技术网

TypeMock到伪DateTime.Parse

TypeMock到伪DateTime.Parse,datetime,typemock,Datetime,Typemock,我需要一种方法来伪造DateTime.Parse和Typemock,并让它在使用任何参数调用时返回相同的日期 我有一个DB字段,它存储一个加密字符串,在加载时解析为日期。保存数据的类有一个Load()方法,它将DB数据复制到其属性中,解密加密的内容并执行一些基本验证,例如: public class BusinessObject{ public DateTime ImportantDate{get;set;} ... public void Load(DBObject dbSou

我需要一种方法来伪造DateTime.Parse和Typemock,并让它在使用任何参数调用时返回相同的日期

我有一个DB字段,它存储一个加密字符串,在加载时解析为日期。保存数据的类有一个Load()方法,它将DB数据复制到其属性中,解密加密的内容并执行一些基本验证,例如:

public class BusinessObject{
    public DateTime ImportantDate{get;set;}
...
    public void Load(DBObject dbSource){
        ...
        ImportantDate = DateTime.Parse(DecryptionService.Decrypt(dbSource.ImportantDate));
    }
}
运行时一切都很好

我正在尝试使用TypeMock编写一个单元测试,以使用其加载方法将一些虚假数据加载到BusinessObject中。BusinessObject的属性太多,无法从XML反序列化,但DBObject可以,所以我存储了一些表示有效数据的XML

在调用DecryptionService对数据进行解密之前,这一切都可以正常工作—它无法工作,因为我的开发人员机器没有加密过程中使用的DB证书。我不能把它们放在我的机器上只是为了测试,那将是一个安全漏洞

我在单元测试中添加了以下内容:

Isolate.Fake.StaticMethods<DecryptionService>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => DecryptionService .Decrypt(null)).WillReturn("***");

Isolate.Fake.StaticMethods<DateTime>(Members.ReturnNulls);
Isolate.WhenCalled(() => DateTime.Parse("***"  /*DateStr*/)).WillReturn(DateTime.Now.AddYears(2));
Isolate.Fake.StaticMethods(Members.ReturnRecursiveFakes);
隔离.WhenCalled(()=>DecryptionService.Decrypt(null)).WillReturn(***);
隔离.false.StaticMethods(Members.ReturnNulls);
隔离.WhenCalled(()=>DateTime.Parse(“***”/*DateStr*/).WillReturn(DateTime.Now.AddYears(2));
DecryptionService被伪造的第一部分起作用,社会安全和其他敏感字符串正在“解密”,但无论我给DateTime提供什么参数,我仍然会得到一个或另一个异常(ArgumentNullException:如果DateStr为null,字符串引用未设置为对象的实例;如果DateStr为“*”,则FormatException)


我如何(如果)覆盖DateTime.Parse(使用typemock),以便它返回有效的DateTime,并传递任何无效参数?

我的名字是Nofar,来自typemock的支持团队

WhenCalled API中不支持DateTime.Parse,因此为了伪造其返回值,需要使用类中的方法对其进行包装,例如:

public class BusinessObject
{
    public DateTime Load (string s)
    {
    return DateTime.Parse(s);  
    }
}
您的测试将如下所示:

    [TestMethod]
    public void TestMethodDateTime()
    {
        BusinessObject b = new BusinessObject();
        DateTime now= DateTime.Now.AddYears(2);

        Isolate.WhenCalled(()=>b.Load(null)).WillReturn(now);

        Assert.AreEqual(now, b.Load(null));
    }
支持DateTime.Parse,WhenCalled API在我们的积压工作中

请随时通过邮件联系我们support@typemock.com

诺法尔
Typemock支持

我的名字是Nofar,来自Typemock的支持团队

WhenCalled API中不支持DateTime.Parse,因此为了伪造其返回值,需要使用类中的方法对其进行包装,例如:

public class BusinessObject
{
    public DateTime Load (string s)
    {
    return DateTime.Parse(s);  
    }
}
您的测试将如下所示:

    [TestMethod]
    public void TestMethodDateTime()
    {
        BusinessObject b = new BusinessObject();
        DateTime now= DateTime.Now.AddYears(2);

        Isolate.WhenCalled(()=>b.Load(null)).WillReturn(now);

        Assert.AreEqual(now, b.Load(null));
    }
支持DateTime.Parse,WhenCalled API在我们的积压工作中

请随时通过邮件联系我们support@typemock.com

诺法尔 Typemock支持