起订量设置InvalidCastException的惩戒实现具有相同的方法签名多个接口的接口时, [英] Moq Setup InvalidCastException when Mocking an interface that implements multiple interfaces having the same method signature
问题描述
所以,我有以下的code:
So I have the following code:
interface Parent1
{
void Foo();
}
interface Parent2
{
void Foo();
}
interface ChildInterface : Parent1, Parent2
{
}
我想嘲笑ChildInterface和设置及其美孚()。所以我用起订量来做到这一点:
I want to mock ChildInterface and setup its Foo(). So I used Moq to do this:
var c = new Mock<ChildInterface>(MockBehavior.Strict);
c.Setup(p1 => ((Parent1)p1).Foo());
c.Setup(p2 => ((Parent2)p2).Foo());
这不能只接受没有做明确的铸造。从<一个href="http://stackoverflow.com/questions/19931702/ambiguous-call-on-an-actiont-where-t-inherits-2-interfaces-having-the-same-met">explanations从这个太问题。所以,我做到了。它编译没有错误!
It cannot just accept without doing an explicit casting. From explanations from this SO question. So I did that. And it compiles without errors!
但一旦运行,它抛出一个 InvalidCastException的
But upon running it, it throws an InvalidCastException
下面是堆栈跟踪:
at lambda_method(Closure )
at Moq.Mock.GetInterceptor(Expression fluentExpression, Mock mock)
at Moq.Mock.<>c__DisplayClass19`1.<Setup>b__18()
at Moq.PexProtector.Invoke[T](Func`1 function)
at Moq.Mock.Setup[T](Mock`1 mock, Expression`1 expression, Func`1 condition)
at Moq.Mock`1.Setup(Expression`1 expression)
你有什么想法如何能起订量工作?
Do you have any ideas on how this can work in Moq?
推荐答案
试试这个:
c.As<Parent1>().Setup(p1 => p1.Foo());
c.As<Parent2>().Setup(p2 => p2.Foo());
这篇关于起订量设置InvalidCastException的惩戒实现具有相同的方法签名多个接口的接口时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!