我不确定我会怎么做.
鉴于我有
public interface IFactory<T> where T : new()
{
IWrapper<T> GetT(string s);
}
public interface IWrapper<out T> where T : new()
{
void Execute(Action<T> action);
}
Run Code Online (Sandbox Code Playgroud)
当我这样做
public class MoqTest
{
public void test()
{
Mock<IWrapper<basicClass>> wrapperMock = new Mock<IWrapper<basicClass>>();
Mock<IFactory<basicClass>> factoryMock = new Mock<IFactory<basicClass>>()
.Setup(p => p.GetT(It.IsAny<string>()))
.Returns(wrapperMock.Object);
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了
无法隐式转换
Moq.Language.Flow.IReturnsResult<TestNamespace.IFactory<TestNamespace.basicClass>>为Moq.Mock<TestNamespace.IFactory<TestNamespace.basicClass>>.存在显式转换(您是否错过了演员?)
请注意,这些只是模拟的示例对象.
它似乎不考虑返回类型等价.一个是a IReturnResult,另一个是aMoq.Mock
您的问题基本上是您将调用Returns方法的结果分配给factoryMock变量.
您想首先创建模拟并将其分配给factoryMock变量,然后像这样设置模拟:
Mock<IWrapper<basicClass>> wrapperMock = new Mock<IWrapper<basicClass>>();
Mock<IFactory<basicClass>> factoryMock = new Mock<IFactory<basicClass>>();
factoryMock
.Setup(p => p.GetT(It.IsAny<string>()))
.Returns(wrapperMock.Object);
Run Code Online (Sandbox Code Playgroud)