我有以下界面
public interface IInfo
{
bool IsCompatibleWith (Object informationObject);
}
public interface IInfo<T> : IInfo
{
bool IsCompatibleWith (T informationObject);
}
并尝试执行以下Mocks
Foo f = new Foo();
Mock<IInfo<Foo>> infoMock = new Mock<IInfo<Foo>>();
infoMock.Setup(i => i.IsCompatibleWith(f)).Returns(true);
然后测试运行以下行
IInfo mockedInfo;
mockedInfo.IsCompatibleWith(f);
问题是,Setup方法设置IsCompatibleWith (T informationObject)
,而代码调用IsCompatibleWith (Object informationObject)
。如何设置这两个签名?
答案 0 :(得分:7)
以下代码段显示了配置这两种方法的方法:
//configure the method with the `object` as a parameter
infoMock.Setup(i => i.IsCompatibleWith((object)f)).Returns(true);
//configure the method with the `IModel` as a parameter
infoMock.Setup(i => i.IsCompatibleWith(f)).Returns(true);
Moq
按原样记录参数。当您将实例投射到object
时,方法bool IsCompatibleWith(Object informationObject)
将接受注册