最小起订量:最小起订量是设置但不认为它被称为

Moq: Moq is setup but doesn't think it is called

我有以下 class 和用于测试目的的 Log 函数 returns 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 日志方法

中调用 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;
    }
}