Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟实现具有相同方法签名的多个接口的接口时的Moq设置InvalidCastException_C#_Interface_Moq_.net - Fatal编程技术网

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