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



我有以下带有 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日志方法中调用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;
    }
}

最新更新