具有 2 个参数的 .Net 中的最小起订量

Moq in .Net with 2 parameters

我正在尝试为 运行 我的测试创建一个 Mock,但出现以下错误, "Invalid callback. Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1)" 这是我的设置函数

  private void Setup()
    {
        this.dataFactoryMock = new Mock<CommonDataFactory>();
        var commonDataFactory = new CommonDataFactory();
        this.dataFactoryMock.Setup(factory => factory.Factory(It.IsAny<DateTime>(), It.IsAny<DateTime>())).Returns<DateTime>(date => commonDataFactory.Factory(date, date));
    }

public class CommonDataFactory
{
    public virtual CommonData Factory(DateTime adjustedAnalysisDate, DateTime analysisDate)
    {            
        var downloadCommonData = CommonData.DownloadCommonData(adjustedAnalysisDate, analysisDate);
        this.cache.Add(key, downloadCommonData, new CacheItemPolicy());
        return downloadCommonData;
    }
}

如果我在 Factory 中只使用一个参数,它工作正常。有人可以帮忙吗?

Returns中使用的表达式需要匹配设置中使用的匹配器数量

设置中有两个日期时间参数匹配器,因此需要在 returns 表达式中使用两个。

this.dataFactoryMock
    .Setup(factory => factory.Factory(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
    .Returns((DateTime adjustedAnalysisDate, DateTime analysisDate) => 
        commonDataFactory.Factory(adjustedAnalysisDate, analysisDate)
    );