I am in the second iteration of an Agile project developing a SharePoint solution and I am struggling with Unit Testing our SharePoint code.
I am trying to Mock up a SPListItemCollection with a specific structure: ID, Name, Department, Division, and I want to populate the list with test data so I can Unit Test our code.
Here is where I get stuck.
I have the following example working but I am at a loss as to how I create the fields and populate the list. I would appreciate any help on this.
var spWeb = Mock.Create<SPWeb>();
var spList = Mock.Create<SPList>();
var spListCollection = Mock.Create<SPListCollection>();
var spListItemCollection = Mock.Create<SPListItemCollection>();
Mock.Arrange(() => spWeb.Lists).Returns(spListCollection);
Mock.Arrange(() => spListCollection[Arg.AnyString]).Returns(spList);
Mock.Arrange(() => spList.GetItems(Arg.IsAny<SPQuery>())).Returns(spListItemCollection);
Assert.AreEqual(spListCollection, spWeb.Lists);
Assert.AreEqual(spList, spWeb.Lists["myList"]);
Assert.AreEqual(spListItemCollection, spWeb.Lists["myList"].GetItems(new SPQuery()));
Many thanks
Gary
I am trying to Mock up a SPListItemCollection with a specific structure: ID, Name, Department, Division, and I want to populate the list with test data so I can Unit Test our code.
Here is where I get stuck.
I have the following example working but I am at a loss as to how I create the fields and populate the list. I would appreciate any help on this.
var spWeb = Mock.Create<SPWeb>();
var spList = Mock.Create<SPList>();
var spListCollection = Mock.Create<SPListCollection>();
var spListItemCollection = Mock.Create<SPListItemCollection>();
Mock.Arrange(() => spWeb.Lists).Returns(spListCollection);
Mock.Arrange(() => spListCollection[Arg.AnyString]).Returns(spList);
Mock.Arrange(() => spList.GetItems(Arg.IsAny<SPQuery>())).Returns(spListItemCollection);
Assert.AreEqual(spListCollection, spWeb.Lists);
Assert.AreEqual(spList, spWeb.Lists["myList"]);
Assert.AreEqual(spListItemCollection, spWeb.Lists["myList"].GetItems(new SPQuery()));
Many thanks
Gary