This is a migrated thread and some comments may be shown as answers.

NullReferenceException mocking SharePoint SPListItem adding item to list

3 Answers 143 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 17 Mar 2011, 05:10 AM

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 57

For 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?

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 18 Mar 2011, 03:57 PM
Hi Mike,

Thanks again for sending the issue. However, SpList.Items.Add creates a new instance of SpListtem and UpdateInternal tries to get the SpItem.Web  which is different from the one that is set and thus fails the test. One way to get this around is by adding the following line:

Mock.Arrange(() => listItems.Add(string.Empty, SPFileSystemObjectType.Folder, string.Empty)).IgnoreArguments().Returns(li);

Using this test will actually pass but originally won't update the Items collection since it is faked and not going through the original flow. In that regard, another way is to use a common collection and adding items to it using DoInstead during SpList.Items.Add and setting the collection in the following way:

IList<SPListItem> items = new List<SPListItem>(); 
Mock.Arrange(() => spList.Items).ReturnsCollection(items);

In this way, you will be able to iterate through the collection and add items to it.

Finally, I would recommend you to take a look at the following post that shows how to create a custom iterative SpListItem.

http://www.telerik.com/community/forums/justmock/general-discussions/mocking-splistitemcollection.aspx


Hope this information is useful. Should you have any more questions please don’t hesitate to write us back.

Kind Regards,
RIcky
the Telerik team
0
Mike
Top achievements
Rank 1
answered on 22 Mar 2011, 01:25 AM
Thanks for responding so promptly. I have a couple of issues though.

1. List<SPListItem> != SPListItemCollection. In my example, I am creating sub-folders of the List and need to make assertions about what the contents of the list are. I can't do this with a generic List<T>.

2. I'm using the Q1 2011 build and only get a reference for a generic ReturnsCollection<T,U>() method. I am not getting a ReturnsCollection() method at all. There is no reference to this in the API documentation either.

Regards
Mike
0
Ricky
Telerik team
answered on 23 Mar 2011, 05:49 PM
Hi Mike,

Thanks for the reply. However, SpListItemCollection implements ICollection which is on the other hand implements IEnumerable and ReturnsCollection actually mocks IEnumerable collection using the target one.

In that regard, the following line is valid:

IList<SPListItem> items = new List<SPListItem>();  
Mock.Arrange(() => spList.Items).ReturnsCollection(items);

I would recommend you to try the sample project from this url:
http://www.telerik.com/community/forums/justmock/general-discussions/mocking-splistitemcollection.aspx

If you have Q1 2011 installed, you will just have to update the JustMock reference and things will work as expected.

Hope this solves your issue. Should you have any more questions please don’t hesitate to write us back.


Kind Regards,
Ricky
the Telerik team
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Mike
Top achievements
Rank 1
Share this question
or