我有一个带有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
bool结果未设置为false,但为true。这让我相信我的设置不正确。是这种情况吗?
答案 0 :(得分:1)
那是因为你没有调用注入logger实例的Log()
方法。在logger.Log()
日志方法
SomeClass
public SomeClass : ILogger
{
private ILogger logger;
// Other functions
public SomeClass(ILogger logger)
{
this.logger = logger;
}
public bool Log()
{
return logger.Log();
//return true;
}
}