chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Error - Method get_Web...

0 votes

Hi Team,

Am trying to create unit test for a SharePoint method but getting get_Web related error- TypeMock.TypeMockException :*** Method get_Web in type Microsoft.SharePoint.Client.ClientContext returns Microsoft.SharePoint.Client.Web and not Microsoft.SharePoint.Client.Web., PFB the unit test I have written. While debugging, it fails at assert.equal statement, it would be helpful if someone can point out where am getting wrong.

public static UsersFromGroupInfo GetAllInfo(string vendorName, string role, ClientContext clientContext)
//Unit Test
        public void Test_To_Get_All_Info()
            ClientContext context = Isolate.Fake.NextInstance<ClientContext>(); //fake the client context
            List lstGroupMappingDetails = Isolate.Fake.Instance<List>(); //fake the list object
            Isolate.WhenCalled(() => context.Web.Lists.GetByTitle("GroupMappingOfInfo")).WillReturn(lstGroupMappingDetails);
            ListItem fakeListItem = Isolate.Fake.Instance<ListItem>();
            Isolate.WhenCalled(() => fakeListItem["Title"]).WithExactArguments().WillReturn("Manager");
            Isolate.WhenCalled(() => fakeListItem["Vendor"]).WithExactArguments().WillReturn("Dummy");
            Isolate.WhenCalled(() => fakeListItem["GroupName"]).WithExactArguments().WillReturn("Grp_Mgr");
            List<ListItem> listItems = new List<ListItem> { fakeListItem };
            Isolate.WhenCalled(() => lstGroupMappingDetails.GetItems(null)).WillReturnCollectionValuesOf(listItems);
            Isolate.WhenCalled(() => context.Load(lstGroupMappingDetails)).IgnoreCall(); //Ignore loading of context
            Isolate.WhenCalled(() => context.ExecuteQuery()).IgnoreCall(); //Ignore execution of SPQuery
            Group group = Isolate.Fake.Instance<Group>(); //fake the group object
            Isolate.WhenCalled(() => context.Web.SiteGroups.GetByName("Grp_Mgr")).WillReturn(group);
            Isolate.WhenCalled(() => context.Load(group)).IgnoreCall(); //Ignore loading of context
            Isolate.WhenCalled(() => context.ExecuteQuery()).IgnoreCall();
            Collection<string> groupUser = new Collection<string>();
            Assert.AreEqual(groupUser, PortalCommon.GetAllInfo("Manager", "Dummy", context));


asked Jan 19, 2017 by ashish5820 (6,100 points)

1 Answer

0 votes

Hi Ashish,

ClientContext context = Isolate.Fake.NextInstance<ClientContext>(); //fake the client context

By this you are creating a handle for the next ClientContext instance.

Try to use instead:

ClientContext context = Isolate.Fake.AllInstances<ClientContext>(); //fake the client context

answered Jan 23, 2017 by eva (1,580 points)