Initialize collections

3 posts, 0 answers
  1. Bernd
    Bernd avatar
    1 posts
    Member since:
    Jun 2011

    Posted 23 Jun 2011 Link to this post

    Hi folks,

    I am trying to use JustMock to mock a SharePoint collection (the real thing cannot be used as it fails without a SharePoint context). I cannot figure out how to add items the collection. Here is my code.
    var uut = Mock.Create<SPRoleDefinitionBindingCollection>();
    var role = Mock.Create<SPRoleDefinition>();
    Mock.Arrange(() => role.BasePermissions)
        .Returns(SPBasePermissions.ViewUsageData | SPBasePermissions.EditListItems | SPBasePermissions.DeleteListItems);
    var result = uut.ContainsPermission(SPBasePermissions.DeleteListItems);

    I would like to add the SPRoleDefinition instance called role to the SPRoleDefinitionBindingCollection. The uut.Add call is obviously being ignored, which is indicated by the fact that uut.Count is zero. I would expect this to work...

    Can you help?

    Thanks in advance and kr, Bernd.
  2. Ricky
    Ricky avatar
    467 posts

    Posted 27 Jun 2011 Link to this post

    Hi Bernd,
    Thanks again for sending the issue. Here you can create a fake list of SPRoleDefinition and use it in DoInstead and Returns to simulate add items in the following way:

        var role = Mock.Create<SPRoleDefinition>();
        var uut = Mock.Create<SPRoleDefinitionBindingCollection>();
        IList<SPRoleDefinition> roles = new List<SPRoleDefinition>();
        Mock.Arrange(() => role.BasePermissions).Returns(SPBasePermissions.ViewUsageData | SPBasePermissions.EditListItems | SPBasePermissions.DeleteListItems);
        Mock.Arrange(() => uut.Count).Returns(() => roles.Count);
        Mock.Arrange(() => ((IEnumerable) uut).GetEnumerator()).Returns(() => roles.GetEnumerator());
        Mock.Arrange(() => uut.Add(role)).DoInstead((SPRoleDefinition r) => roles.Add(r));
        Mock.Arrange(() => uut.Contains(role)).Returns((SPRoleDefinition r) => roles.Contains(r));
        Assert.IsTrue(uut.Count == 1);
        bool inLoop = false;   
        foreach (SPRoleDefinition spRoleDefinition in uut)
            inLoop = true;

    In addition i am delegating the GetEnumerator call for the target SPRoleDefinitionBindingCollection to the custom list which is invoked during foreach and gets the item added during uut.Add

    KInd Regards,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Bernd Rickenberg
    Bernd Rickenberg avatar
    2 posts
    Member since:
    Sep 2008

    Posted 04 Aug 2011 Link to this post

    Hi Ricky,

    That is lovely. Thanks for your help. Kr, Bernd.
Back to Top