我正在使用 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!
有任何想法吗?