[英]Moq doesn't match methods. Moq.MockException: All invocations on the mock must have a corresponding setup
[英]Moq: Moq is setup but doesn't think it is called
我有一个带有Log函数的以下类,出于测试目的,该函数仅返回true。
public SomeClass : ILogger
{
// Other functions
public bool Log()
{
return true;
}
}
在单元测试中,我有以下几点:
Mock<ILogger> logger = new Mock<ILogger>();
logger.Setup(func => func.Log()).Returns(() => false).Verifiable();
SomeClass testMe = new SomeClass(logger.Object);
bool result = testMe.Log();
logger.Verify(); //This fails saying that the Log function was never called
布尔结果不是设置为false,而是设置为true。 这使我相信我的设置不正确。 是这样吗
那是因为您尚未调用注入的记录器实例的Log()
方法。 在SomeClass
Log方法中调用logger.Log()
public SomeClass : ILogger
{
private ILogger logger;
// Other functions
public SomeClass(ILogger logger)
{
this.logger = logger;
}
public bool Log()
{
return logger.Log();
//return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.