3

我正在使用 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!

有任何想法吗?

4

1 回答 1

0

我解决了它在设置方法中更改为 long 的问题,因为 GetById 接受一个 long 参数。呃!

于 2012-11-28T14:18:57.823 回答