I’ve got a method which takes a IList<Person> and returns an IEnumberable as so:
internal static IEnumerable<Dictionary<string, string>> GetPersonsPerSite(IList<Person> Data)
{
//Implementation ...
}
and I’m trying to create a mock object of IList<Person> so I can test this method.
Using Moq I have written the following:
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"
});
However when I come to use the object the IEnumerable returned is aways throwing exception Object reference not set to an instance of an object.
I’m brand new to Moq, and I’m pretty sure I’m missing a fundamental concept here, however I’ve successfully being able to throw exceptions and modifiy outputs with other mock objects.
I’d appreciate if someone could point me in the right direction.
Don’t mock the IList. You don’t need to, unless there’s something specific your looking to check.
Instead just keep your test simple and do something like this: