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)
        {
            //content
        }
 
//Unit Test
 
        [TestMethod]
        [Isolated]
        public void Test_To_Get_All_Info()
        {
            //Arrange
 
            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>();
 
            groupUser.Add(fakeListItem["Title"].ToString());
 
            //Assert
 
            Assert.AreEqual(groupUser, PortalCommon.GetAllInfo("Manager", "Dummy", context));
 
        }

 

asked Jan 19 by ashish5820 (140 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 by eva (1,580 points)
...