Moq: Verifying a method was called with a particul

2019-03-03 12:27发布

问题:

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

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?


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


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.

回答1:

Mono bug: https://bugzilla.novell.com/show_bug.cgi?id=656918.