Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
C# 通过Moq设置匹配参考类型参数_C#_.net_Unit Testing_Vs Unit Testing Framework_Moq 3 - Fatal编程技术网

C# 通过Moq设置匹配参考类型参数

C# 通过Moq设置匹配参考类型参数,c#,.net,unit-testing,vs-unit-testing-framework,moq-3,C#,.net,Unit Testing,Vs Unit Testing Framework,Moq 3,我有类似的问题,一问或,但解决方案似乎不适用于我的情况 考虑以下类别: public interface IProductsWebService { ICObjectLang[] GetClassLevel(getClassLevelLang criteria); } // the rest of this class was automaticaly generated as web service reference public partial class getClassLev

我有类似的问题,一问或,但解决方案似乎不适用于我的情况

考虑以下类别:

public interface IProductsWebService
{
    ICObjectLang[] GetClassLevel(getClassLevelLang criteria);
}

// the rest of this class was automaticaly generated as web service reference
public partial class getClassLevelLang : IEquatable<getClassLevelLang>
{
    public override bool Equals(object obj)
    {
        var other = obj as getClassLevelLang;

        return Equals(other);
    }

    public bool Equals(getClassLevelLang other)
    {
        if (this == other)
        {
            return true;
        }

        if (other == null)
        {
            return false;
        }

        return CID == other.CID &&
               accessCode == other.accessCode &&
               classCode == other.classCode &&
               depth == other.depth &&
               language == other.language &&
               partyCode == other.partyCode &&
               refId == other.refId;
    }

    public override int GetHashCode()
    {
        var hash = 531;

        hash += CID != null ? CID.GetHashCode() : 1;
        hash += accessCode != null ? accessCode.GetHashCode() : 1;
        hash += classCode != null ? classCode.GetHashCode() : 1;
        hash += depth != null ? depth.GetHashCode() : 1;
        hash += language != null ? language.GetHashCode() : 1;
        hash += partyCode != null ? partyCode.GetHashCode() : 1;
        hash += refId != null ? refId.GetHashCode() : 1;

        return hash;
    }

    public override string ToString()
    {
        var str = new StringBuilder();

        str.AppendFormat("CID: {0}, ", CID);
        str.AppendFormat("accessCode: {0}, ", accessCode);
        str.AppendFormat("classCode: {0}, ", classCode);
        str.AppendFormat("depth: {0}, ", depth);
        str.AppendFormat("language: {0}, ", language);
        str.AppendFormat("partyCode: {0}, ", partyCode);
        str.AppendFormat("refId: {0}", refId);

        return str.ToString();
    }
}
上述测试失败,但以下情况除外:

Test method ProductsImport.Console.Test.ProductsImportProcessUnitTest.RecursiveDownloadTest threw exception: 
Moq.MockVerificationException: The following setups were not matched:
    IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: , accessCode: xyz, classCode: tree1, depth: 1, language: all, partyCode: something, refId: )))
    IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: cat1.1.1, accessCode: xyz, classCode: tree1, depth: 1, language: all, partyCode: something, refId: )))
    IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: , accessCode: xyz, classCode: tree2, depth: 1, language: all, partyCode: something, refId: )))
Result StackTrace:    
    at Moq.Mock.VerifyAll()
测试方法ProductsImport.Console.Test.ProductsImportProcessUnitTest.RecursiveDownloadTest引发异常:
Moq.MockVerificationException:以下设置不匹配:
IPProductsWebService mock=>mock.GetClassLevel(It.Is(c=>c.Equals(CID:,accessCode:xyz,classCode:tree1,depth:1,language:all,partyCode:something,refId:))
IPProductsWebService mock=>mock.GetClassLevel(It.Is(c=>c.Equals(CID:cat1.1.1,accessCode:xyz,classCode:tree1,depth:1,language:all,partyCode:something,refId:))
IPProductsWebService mock=>mock.GetClassLevel(It.Is(c=>c.Equals(CID:,accessCode:xyz,classCode:tree2,depth:1,language:all,partyCode:something,refId:))
结果跟踪:
在Moq.Mock.VerifyAll()时

我确信ProductsWebService.GetClassLevel正在被调用,而
getClassLevelLang.Equals
没有被调用(通过调试器检查)。更奇怪的是,就在调用
VerifyAll()
列表之前,我检查了
调用的实际值,它是空的。

您的
产品服务
模拟没有被注入到
产品端口进程
中,因此无法拦截对实际
IPProductsWebService
的调用。如何在代码中实例化
IProductsWebService
对象?

您的
productsService
mock没有被注入到
productsPortProcess
中,因此无法拦截对实际
IProductsWebService
的调用。如何在代码中实例化
ipProductsWebService
对象?

实际上,它是通过构造函数注入到上面的代码中的:
target=new ProductsSimportProcess(…,productsService.object,…)在我的应用程序中,它是由IoC容器(Ninject)注入的,但是,根据上面的代码,您在注入它之后实例化它,所以当它被注入到您的测试中时,它将是null是的,您明白了——在收到第一个这样的错误后,我已经多次更改代码了(在实现
getClassLevelLang
中的自定义相等逻辑之前)我忽略了这一点。新手犯了很多错误…Thx!实际上它是由构造函数在上面的代码中注入的:
target=new ProductsImportProcess(…,productsService.Object…);
在我的应用程序中它是由IoC容器(Ninject)注入的好的,但是,根据上面的代码,您在注入它之后实例化了它,所以当它被注入到您的测试中时,它将是空的。是的,您明白了——在收到第一个这样的错误之后(在
getClassLevelLang
中实现自定义相等逻辑之前),我已经多次更改了代码我错过了。菜鸟的错误…太多了!
Test method ProductsImport.Console.Test.ProductsImportProcessUnitTest.RecursiveDownloadTest threw exception: 
Moq.MockVerificationException: The following setups were not matched:
    IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: , accessCode: xyz, classCode: tree1, depth: 1, language: all, partyCode: something, refId: )))
    IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: cat1.1.1, accessCode: xyz, classCode: tree1, depth: 1, language: all, partyCode: something, refId: )))
    IProductsWebService mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(CID: , accessCode: xyz, classCode: tree2, depth: 1, language: all, partyCode: something, refId: )))
Result StackTrace:    
    at Moq.Mock.VerifyAll()