Mocking SPListItemCollection

2 posts, 0 answers
  1. Gary
    Gary avatar
    2 posts
    Member since:
    Jan 2011

    Posted 09 Mar 2011 Link to this post

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

    Posted 10 Mar 2011 Link to this post

    Hi Gary,

    Thanks again for sending the issue. To demonstrate how to return custom predefined SpListItem, I have written a sample test that first adds an item to SpListItemCollection and then asserts it accordingly.

    To begin, I created the following method that prepares a fake SpListItem from user given values for ID, Name, Department and Division:

    private SPListItem CreateEntity(Guid id, string name, string department, string division)  
    {  
        var fakeItem = Mock.Create<SPListItem>(); 
        Mock.Arrange(() => fakeItem["ID"]).Returns(id);
        Mock.Arrange(() => fakeItem["Name"]).Returns(name);
        Mock.Arrange(() => fakeItem["Department"]).Returns(department);
        Mock.Arrange(() => fakeItem["Division"]).Returns(division);  
        
        return fakeItem;      
    }

    Inside the test method, first I created the mock instance of SpWeb, SpList and SpListCollection.

    var spWeb = Mock.Create<SPWeb>();
    var spList = Mock.Create<SPList>();
    var spListCollection = Mock.Create<SPListCollection>();

    Moving forward, Justmock has a method for setting fake collections named ReturnsCollection that is located under Telerik.Justmock.Helpers namespace and that actually does some initialization on behalf to make mocking of collections simpler.

    Once I am done including the namespace, I added a list that contains the entity created using the CreateEntity method shown above.

    IList<SPListItem> items = new List<SPListItem>();
    items.Add(CreateEntity(Guid.NewGuid(), "Telerik", "Support", "IT"));

    Next, I set the list to SpListItemCollection using ReturnsCollection which is on the other assigned to SpWeb and that returns mocked SpList for any key value:

    Mock.Arrange(() => spList.Items).ReturnsCollection(items);
    Mock.Arrange(() => spWeb.Lists).Returns(spListCollection);
    Mock.Arrange(() => spListCollection[Arg.AnyString]).Returns(spList);

    Finally, I asserted If the SpListItemCollection contains the item as expected:

    Assert.AreEqual(1, spWeb.Lists["myList"].Items.Count);

    However, there was an issue with the internal build that is sent to you earlier. Therefore, if you require one please open up a support ticket as before and we will send you a new one (which is actually the Q1 2011 build to be released soon). The previous build will still work for iterating the items but will fail for ICollection.Count call that is shown above.

    Moreover, the sample project is attached for you to take a look and please do write us for any further exceptions that you might face.


    Kind Regards,
    Ricky
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. DevCraft R3 2016 release webinar banner
Back to Top