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

fake Getfields returning empty list

3 Answers 112 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rico
Top achievements
Rank 1
Rico asked on 13 May 2019, 02:44 PM

Hi

I try to fake the getitems method on a list i the sharepoint clientcontext. For some reason its always returning no elements. Can someone put me in the right direction?

ClientContext context = Isolate.Fake.NextInstance<ClientContext>();
            List list = Isolate.Fake.Instance<List>();
            ListItem fakeListItem = Isolate.Fake.Instance<ListItem>();
            List<ListItem> listItems = new List<ListItem> { fakeListItem, fakeListItem, fakeListItem };
 
            Isolate.WhenCalled(() => list.GetItems(null)).WillReturnCollectionValuesOf(listItems);
            Isolate.WhenCalled(() => context.Web.Lists.GetByTitle(string.Empty)).WillReturn(list);
            Isolate.WhenCalled(() => context.Load(list)).IgnoreCall();
            Isolate.WhenCalled(() => context.ExecuteQuery()).IgnoreCall();
 
            // Act
            var cut = new ClassUnderTest();
            int itemsCount = cut.GetListItemsCount(string.Empty, string.Empty);

           

Here itemsCount returns always 0 elements. Am I using a wrong context/instance?

 

GetListItemsCount is doing the following

using (ClientContext clientContext = new ClientContext(siteUrl))
                {
                    List theList = clientContext.Web.Lists.GetByTitle(listTitle);
                    CamlQuery q = CamlQuery.CreateAllItemsQuery();
                    var items = theList.GetItems(q);
                    clientContext.Load(theList);
                    clientContext.Load(items);
 
                    clientContext.ExecuteQuery();
 
                    return items.Count;
                }

 

Nothing special so far.

 

3 Answers, 1 is accepted

Sort by
0
Ivo
Telerik team
answered on 14 May 2019, 08:52 AM
Hello Rico,

I am afraid that I could not be able to help you, since the question is related to the product of our competitors. Please forward your request to the Typemock support.
 
If you consider a migration to Telerik JustMock or just curious about it, visit our product site. I hope that you enjoy it.

Have a great day,
Ivo
Progress Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Rico
Top achievements
Rank 1
answered on 14 May 2019, 06:55 PM

Hello Ivo

 

Thanks for noticing. ;)

I am obviously evaluating multiple frameworks atm. And i copy and pasted the wrong code. I am so sorry.

here is the correct one.

 

int expectedCount = 3;
 
            var mockCtx = Mock.Create<ClientContext>(Constructor.Mocked);
            var mockWeb = Mock.Create<Web>();
            var mockList = Mock.Create<List>();
            var mockListitem = Mock.Create<ListItem>();
            var camlQ = Mock.Create<CamlQuery>();
            var listItems = new List<ListItem> { mockListitem, mockListitem, mockListitem };
            listItems.Add(mockListitem);
            //var listItems1 = Mock.Create<ListItemCollection>();
 
            // Arranges the constructor of every new instance of ClientContext.
            Mock.Arrange(() => new ClientContext(Arg.AnyString)).DoNothing();
            // Arranges every new call to ClientContext.Web to return our mockWeb.
            Mock.Arrange(() => mockCtx.Web).IgnoreInstance().Returns(mockWeb);
 
            // Arranges mockWeb.Lists to return our fake collection.
            Mock.Arrange(() => mockCtx.Web.Lists.GetByTitle(Arg.AnyString)).Returns(mockList);
            Mock.Arrange(() => mockList.GetItems(null)).ReturnsCollection(listItems);
 
 
            // Arranges every new call to ClientContext.Load no matter the arguments to do nothing.
            Mock.Arrange(() => mockCtx.Load(Arg.IsAny<List>())).IgnoreInstance().DoNothing();
            Mock.Arrange(() => mockCtx.Load(Arg.IsAny<ListItemCollection>())).IgnoreInstance().DoNothing();
            // Arranges every new call to ClientContext.ExecuteQuery to do nothing.
            Mock.Arrange(() => mockCtx.ExecuteQuery()).IgnoreInstance().DoNothing();
 
 
            var cut = new ClassUnderTest();
            int itemsCount = cut.GetListItemsCount(string.Empty, string.Empty);
 
            Assert.AreEqual(expectedCount, itemsCount);

 

In this case itemsCount is always 0.

For this simple example it looks like a lot to Mock. Is there a simpler, more efficient way to do it?

Also is there more examples how to work with SharePoint SSOM or CSOM?

 

Thanks in advance.

0
Ivo
Telerik team
answered on 15 May 2019, 02:58 PM
Hello Rico,

I have made a sample project with the code provided and looked on it in deep. The issue with the counter is caused by the mock returned from the following arrangement:

Mock.Arrange(() => mockList.GetItems(Arg.IsAny<CamlQuery>())).ReturnsCollection(listItems);
 
This particular arrangement mocks IEnumarable interface derived methods, but stubs the rest ones. That is why Count property of ClientObjectCollection class returns always 0. The easiest way to fix this is to use future mocking like this:

var listItemsCollection = Mock.Create<ListItemCollection>();
Mock.Arrange(() => listItemsCollection.Count).IgnoreInstance().Returns(listItems.Count);


I have also provided an alternative solution in the sample project, which relies on IEnumarable interface. Please take into consideration just for better understanding of the problem, generally it is not recommended.

Regarding SharePoint samples you can find an article in our documentation, but I find it a bit outdated and uncompleted. It is a good topic for further improvements.

I hope the provided information answers your questions. If you need any further assistance do not hesitate to write us back.

Regards,
Ivo
Progress Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Rico
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Rico
Top achievements
Rank 1
Share this question
or