NullReferenceException mocking SharePoint SPListItem adding item to list

4 posts, 0 answers
  1. Mike
    Mike avatar
    2 posts
    Member since:
    Jun 2009

    Posted 17 Mar 2011 Link to this post

    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?
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 18 Mar 2011 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Mike
    Mike avatar
    2 posts
    Member since:
    Jun 2009

    Posted 21 Mar 2011 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 Mar 2011 Link to this post

    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
Back to Top