C# 模拟实现具有相同方法签名的多个接口的接口时的Moq设置InvalidCastException
因此,我有以下代码:C# 模拟实现具有相同方法签名的多个接口的接口时的Moq设置InvalidCastException,c#,interface,moq,.net,C#,Interface,Moq,.net,因此,我有以下代码: interface Parent1 { void Foo(); } interface Parent2 { void Foo(); } interface ChildInterface : Parent1, Parent2 { } 我想模拟ChildInterface并设置它的Foo()。所以我用最小起订量来做这个: var c = new Mock<ChildInterface>(MockBehavior.Strict); c.Setup
interface Parent1
{
void Foo();
}
interface Parent2
{
void Foo();
}
interface ChildInterface : Parent1, Parent2
{
}
我想模拟ChildInterface并设置它的Foo()。所以我用最小起订量来做这个:
var c = new Mock<ChildInterface>(MockBehavior.Strict);
c.Setup(p1 => ((Parent1)p1).Foo());
c.Setup(p2 => ((Parent2)p2).Foo());
您对如何在Moq中使用此功能有什么想法吗?试试这个:
c.As<Parent1>().Setup(p1 => p1.Foo());
c.As<Parent2>().Setup(p2 => p2.Foo());
c.As().Setup(p1=>p1.Foo());
c、 As().Setup(p2=>p2.Foo());
c.As<Parent1>().Setup(p1 => p1.Foo());
c.As<Parent2>().Setup(p2 => p2.Foo());