I'm trying to mock out adding a folder to a SPList, using the code below:
SPWeb web = Mock.Create<SPWeb>(); SPListCollection lists = Mock.Create<SPListCollection>(); SPList list = Mock.Create<SPList>(); SPListItemCollection listItems = Mock.Create<SPListItemCollection>(); SPListItem li = Mock.Create<SPListItem>(); Mock.Arrange(() => web.Lists).Returns(lists); Mock.Arrange(() => lists[Arg.AnyString]).Returns(list); Mock.Arrange(() => list.Items).Returns(listItems); Mock.Arrange(() => list.Folders).Returns(listItems); Mock.Arrange(() => li.Web).Returns(web); li = list.Items.Add("",SPFileSystemObjectType.Folder, "Folder One"); li.Update(); The exception I'm getting when I execute my test in Gallio is a NullReferenceException below:
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.SPListItem.get_Web() at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion,
Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish,
Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) at Microsoft.SharePoint.SPListItem.Update() at CreateFoldersInSpList.Tests.TestSuite.GetSpList()
in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\
CreateFoldersInSpList\CreateFoldersInSpList.Tests\TestSuite.cs:line 57For this reason, I added the Mock.Arrange(() => li.Web).Returns(web) statement but am still getting this exception.
What do I need to do to be able to continue with this test?