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

Initialize collections

2 Answers 78 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bernd
Top achievements
Rank 1
Bernd asked on 23 Jun 2011, 09:42 AM
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);
uut.Add(role);
 
var result = uut.ContainsPermission(SPBasePermissions.DeleteListItems);
Assert.IsTrue(result);

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 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 27 Jun 2011, 02:55 PM
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));
 
    uut.Add(role);
 
    Assert.IsTrue(uut.Count == 1);
    Assert.IsTrue(uut.Contains(role));
 
    bool inLoop = false;   
 
    foreach (SPRoleDefinition spRoleDefinition in uut)
    {
        inLoop = true;
    }
 
    Assert.IsTrue(inLoop);

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,
Ricky
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
0
Bernd Rickenberg
Top achievements
Rank 1
answered on 04 Aug 2011, 02:50 PM
Hi Ricky,

That is lovely. Thanks for your help. Kr, Bernd.
Tags
General Discussions
Asked by
Bernd
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Bernd Rickenberg
Top achievements
Rank 1
Share this question
or