无法验证c#中对mock的调用。

问题描述 投票:0回答:1

我有一个方法调用 SaveDocument() 班里 DocumentStorageHandler

DocumentManagementRequestModel docRequest=new DocumentManagementRequestModel
{
  Parameters = parameters,
  ProviderAlias = providerAlias
};
 _documentSaver.SaveDocument(docRequest);

Parameter 阶级为

public class DmuParameter
{
    public string Name { get; set; }
    public Object Value { get; set; }
    public string ParameterDirection { get; set; }
    public string Category { get; set; }
}

当我试图为它写单元测试时,它却以 "期望在mock上至少调用一次但从未被执行 "而失败。

_mockedDocumentSaver.Setup(d => d.SaveDocument(
            It.Is<DocumentManagementRequestModel>(
                d => d.Parameters.Exists(
                  p => p.Category == "category"
                   && p.Name == "FileLocation"
                   && p.Value.ToString() == "\\\\charon.cmiprog.com\\Devinet\\Documaker\\Api\\Temp\\1234567-00-00000-IdCardsPL.pdf"
                    && p.ParameterDirection == "Input"))));

_mockedDocumentSaver.Verify(c=>c.SaveDocument(It.Is<DocumentManagementRequestModel>(
            d=>d.Parameters.Exists(p=>
                    p.Category == "category"
                    && p.Name == "FileLocation"
                    && p.Value.ToString() == 
"\\\\charon.cmiprog.com\\Devinet\\Documaker\\Api\\Temp\\1234567-00-00000-IdCardsPL.pdf"
                    && p.ParameterDirection == "Input"
                ))),Times.AtLeastOnce);

我在验证对mock的调用时,是不是用了正确的方法。我不知道我错过了什么。

为了得到更多的信息,附上截图。The mock verification screen shotIn code

c# moq
1个回答
0
投票

根据你提供的信息,看起来是Parameter.Value的字符串平等问题。

调用断点截图中的'devinet'为小写。

enter image description here

在您的验证断点截图中,"DevInet "是混合大小写的。

enter image description here

在OP中的setupverify表达式中,是 "Devinet"。

设置。

&& p.Value.ToString() == "\\\\charon.cmiprog.com\\Devinet\\Documaker\\Api\\Temp\\1234567-00-00000-IdCardsPL.pdf"

验证。

&& p.Value.ToString() == "\\\\charon.cmiprog.com\\Devinet\\Documaker\\Api\\Temp\\1234567-00-00000-IdCardsPL.pdf"

我的眼睛会在外壳上运行。CI平等条件可能对你的调试有帮助。

&& p.Value.ToString().Equals("\\\\charon.cmiprog.com\\Devinet\\Documaker\\Api\\Temp\\1234567-00-00000-IdCardsPL.pdf", StringComparison.CurrentCultureIgnoreCase)
© www.soinside.com 2019 - 2024. All rights reserved.