【发布时间】:2012-03-01 17:59:39
【问题描述】:
我有一个方法,它接受 IList<Person> 并返回一个 IEnumberable,如下所示:
internal static IEnumerable<Dictionary<string, string>> GetPersonsPerSite(IList<Person> Data)
{
//Implementation ...
}
我正在尝试创建一个 IList<Person> 的模拟对象,以便我可以测试这个方法。
使用 Moq 我写了以下内容:
var mockObjects = new Mock<IList<Person>>();
mockObjects.Setup(x => x[0]).Returns(new Person()
{
SITE_ID = "test",
MPAN = "test",
ADDLINE1 = "test",
ADDLINE2 = "test",
ADDRESS_LINE_1 = "test",
ADDRESS_LINE_2 = "test"
});
但是,当我开始使用 IEnumerable 返回的对象时,会抛出异常Object reference not set to an instance of an object.
我是 Moq 的新手,我很确定我在这里遗漏了一个基本概念,但是我已经成功地能够抛出异常并使用其他模拟对象修改输出。
如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
标签: c# unit-testing testing mocking moq