通过Moq设置匹配引用类型参数

本文关键字:引用 类型参数 Moq 设置 通过 | 更新日期: 2023-09-27 18:02:10

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

考虑以下类:

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();
    }
}

和以下代码用于测试(calls列表仅用于调试):

private Mock<IProductsWebService> productsService;
private IProductsImportProcess target;
private List<getClassLevelLang> calls = new List<getClassLevelLang>();
[TestMethod]
public void RecursiveDownloadTest()
{
        //other mocks (configuration, log, categoryService and configurationService) set here
        target = new ProductsImportProcess(
            configuration.Object, log.Object, categoryService.Object,
            productsService.Object, configurationService.Object);
        productsService = new Mock<IProductsWebService>(MockBehavior.Strict);
        var root1 = new Category
        {
            CategoryID = "root1",
            SubCategories = new List<Category>
                                                {
                                                    new Category
                                                        {
                                                            CategoryID = "cat1.1",
                                                            SubCategories = new List<Category>
                                                                                {
                                                                                    new Category
                                                                                        {CategoryID = "cat1.1.1"}
                                                                                }
                                                        },
                                                    new Category
                                                        {
                                                            CategoryID = "cat1.2"
                                                        }
                                                }
        };
        var root2 = new Category { CategoryID = "root2" };
        setupProductsServiceCall("tree1", root1, true);
        setupProductsServiceCall("tree1", root1.SubCategories.First().SubCategories.First());
        setupProductsServiceCall("tree2", root2, true);
        target.Execute();
        productsService.VerifyAll();
    }
    private void setupProductsServiceCall(string treeCode, Category category, bool isRoot = false)
    {
        var config = configuration.Object;
        var criteria = new getClassLevelLang
                           {
                               CID = isRoot ? null : category.CategoryID,
                               language = "all",
                               partyCode = "something",
                               depth = "1",
                               classCode = treeCode,
                               accessCode = "xyz"
                           };
        var productsNode = TestUtils.CreateAbbProductsNode(category);
        productsService.Setup(mock => mock.GetClassLevel(It.Is<getClassLevelLang>(c => c.Equals(criteria)))).
            Callback<getClassLevelLang>(c => calls.Add(c)).        //added for debuging purposes only
            Returns(productsNode.objectAttributes).
            Verifiable();
}

上面的测试失败,有以下异常:

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()

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

通过Moq设置匹配引用类型参数

您的productsService模拟没有被注入到您的ProductsImportProcess中,因此无法拦截在实际IProductsWebService上进行的调用。如何在代码中实例化IProductsWebService对象?