1

ここまたはここで尋ねられたものと同様の問題がありますが、私の場合、解決策は当てはまらないようです。

次のクラスを検討してください。

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

テスト用の次のコード ( callslist はデバッグ専用です):

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います (デバッガーで確認)。さらに奇妙なことに、リストをcalls呼び出す直前に実際の値を確認しましたが、VerifyAll()空です。

4

1 に答える 1

1

あなたのproductsServiceモックはあなたに注入されてProductsImportProcessいないので、実際に行われた呼び出しをインターセプトできませんIProductsWebService。コード内でオブジェクトをどのようにインスタンス化しIProductsWebServiceますか?

于 2013-03-04T13:03:49.000 に答える