Moq:验证使用特定委托调用的方法会产生“方法参数长度不匹配”
我有一个类 A
,它调用接口 B
上的方法,并将其自己的方法之一传递给它,作为 B
的延续> 应该在有结果时调用。该代码在实践中似乎运行良好,但我不知道如何使用 Moq 来测试它 - 当我尝试明显的事情时,它会产生 System.ArgumentException :方法参数长度不匹配 。起初我以为这可能是我的代码,但在以下玩具案例中它以同样的方式失败:
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# 来说相对较新,但是测试这个的正确方法是什么?
更新:作为参考,我在 MacOS 10.6.5 上使用 Mono 2.6.7,目标为 .NET 3.5。
再次更新:最好的猜测是这是一个 Mono 错误;我已将其归档为 https://bugzilla.novell.com/show_bug.cgi?id =656918。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单声道错误:https://bugzilla.novell.com/show_bug.cgi?id=656918< /a>.
Mono bug: https://bugzilla.novell.com/show_bug.cgi?id=656918.