我正在使用Moq,我有一个需要创建模拟方法的存储库。 GetAll方法可以返回值,但是GetById返回null。
var currencyRepo = new Mock<ICurrencyRepository>();
var currencies = new List<Currency>{new Currency
{
CurrencyCode = "USD",
CurrencyId = 1,
},
new Currency
{
CurrencyCode = "EUR",
CurrencyId = 2
}};
currencyRepo.Setup(c => c.GetAll()).Returns(currencies);
currencyRepo.Setup(c => c.GetById(It.IsAny<int>())).Returns((int i) => currencies.Single(c => c.CurrencyId == i));
var currency = currencyRepo.Object.GetById(1); //This always returns null
//currency is always null
//but calling the GetAll method works!
var currencyList = currencyRepo.Object.GetAll(); //this works!
有什么想法吗?