Reputation: 465
I am trying to create a Mock to run my tests but I am getting the following error, "Invalid callback. Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1)" Here's my Setup function
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;
}
}
If I use only one parameter in the Factory it works fine. Can someone help please?
Upvotes: 7
Views: 3097
Reputation: 247591
The expression used in the Returns
needs to match the number of matchers used in the setup
There were two date-time argument matchers in the setup so there needs to be two used in the returns expression.
this.dataFactoryMock
.Setup(factory => factory.Factory(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
.Returns((DateTime adjustedAnalysisDate, DateTime analysisDate) =>
commonDataFactory.Factory(adjustedAnalysisDate, analysisDate)
);
Upvotes: 11