Moq:使用特定委托调用验证方法会产生“方法参数长度不匹配” [英] Moq: Verifying a method was called with a particular delegate produces "method argument length mismatch"
问题描述
我有一个类 A
,调用界面 B
上的一个方法,传递给它一个自己的方法作为一种连续性,当 c> 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#来说,我不了解的代表的魔法,对于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 bug;我已将其提交为 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屋!