Moq:验证使用特定委托调用方法会产生“方法参数长度不匹配"; [英] Moq: Verifying a method was called with a particular delegate produces "method argument length mismatch"

查看:24
本文介绍了Moq:验证使用特定委托调用方法会产生“方法参数长度不匹配";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 A,它调用接口 B 上的方法,将它自己的方法之一传递给它,作为 B 的延续 应该在有结果时调用.该代码在实践中似乎运行良好,但我不知道如何使用 Moq 对其进行测试——当我尝试显而易见的事情时,它会产生 System.ArgumentException : 方法参数长度不匹配.起初以为这可能是我的代码,但它与以下玩具箱的失败方式相同:

I've got a class A that calls a method on interface B, passing to it one of its own methods as sort of a continuation that B is supposed to call when it has a result. The code seems to work fine in practice, but I can't figure out how to test it with Moq -- when I try the obvious thing, it produces System.ArgumentException : method argument length mismatch. Thought at first it might be my code, but it fails the same way with the following toy case:

public class A
{
    readonly B myB;

    public A (B b)
    {
        myB = b;
    }

    public void HandleC (C c)
    {
        // do something
    }

    public void DoFindC ()
    {
        myB.FindC (HandleC);
    }
}

public interface B
{
    // Finds a C and then passes it to handleC
    void FindC (Action<C> handleC);
}

public interface C
{
}

[TestFixture()]
public class ATest
{
    [Test()]
    public void TestDoFindC ()
    {
        Mock<B> bMock = new Mock<B> ();
        A a = new A(bMock.Object);
        a.DoFindC();

        bMock.Verify(b => b.FindC(a.HandleC));
    }
}

我猜有一些我还不理解的代理的幕后魔术,我对 C# 相对较新,但是测试这个的正确方法是什么?

I'm guessing there's some behind-the-scenes magic with delegates that I don't understand yet, being relatively new to C#, but what's the right way to test this?

更新:作为参考,我在 MacOS 10.6.5 上使用 Mono 2.6.7 并针对 .NET 3.5.

Update: For reference, I'm using Mono 2.6.7 on MacOS 10.6.5 and targeting .NET 3.5.

再次更新:最好的猜测是这是一个 Mono 错误;我已将其归档为 https://bugzilla.novell.com/show_bug.cgi?id=656918.

Updated again: Best guess is that this is a Mono bug; I've filed it as https://bugzilla.novell.com/show_bug.cgi?id=656918.

推荐答案

单声道错误:https://bugzilla.novell.com/show_bug.cgi?id=656918.

这篇关于Moq:验证使用特定委托调用方法会产生“方法参数长度不匹配";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆