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 Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I have a utility method to create a populated SPList:
        private SPList MakeFakeList(int itemCount)
        {
            SPList fakeList = Isolate.Fake.Instance<SPList>();
            for (int i = 1; i <= itemCount; i++)
            {
                Isolate.WhenCalled(() => fakeList.Items[i])
                    .WillReturn(MakeFakeTaskItem(i, "This is task number " + i.ToString()));
                Isolate.WhenCalled(() => fakeList.GetItemById(i))
                    .WithExactArguments()
                    .WillReturn(MakeFakeTaskItem(i, "This is task number " + i.ToString()));
            }
            return fakeList;
        }


I can return a specific item using either .Items[n] or .GetItemByID(n), and it works fine. But I can't get the .ListCount value. Here's my test:
        [Test]
        public void FakeList_ItemCount()
        {
            SPList spList = MakeFakeList(10);
            Assert.That(spList.ItemCount, Is.EqualTo(10));
        }

It fails with:
LSMSTests.Tests.FakeList_ItemCount:
  Expected: 10
  But was:  0


As a sanity check, I tried this simpler test, which doesn't use my utility method at all:
        [Test]
        public void MakeFakeList_AddItem_ItemCount()
        {
            var fakeList = Isolate.Fake.Instance<SPList>(Members.ReturnRecursiveFakes);
            SPListItem spListItem = fakeList.AddItem();
            Assert.That(fakeList.ItemCount, Is.EqualTo(1));
        }
But it fails also:
LSMSTests.Tests.MakeFakeList_AddItem_ItemCount:
  Expected: 1
  But was:  0
I'm trying to fake a SharePoint list that can have items added to it -- not just have it return a fake list item, but also behave like a real SharePoint list that has had an item added -- i.e. report an accurate .ListCount, and actually contain the newly added item. Am I going beyond what TypeMock is designed to do?
asked by leighwebber (1.2k points)

4 Answers

0 votes
Hi,

This is the expected behavior of a fake object. In this test, SPList is a recursive fake. That means that all methods and properties are faked. The logic in SPList that increments the items counter on item insertion is not called.

As a solution, when faking a collection and depending on property the represents its state, you can fake the property as well:
Isolate.WhenCalled(() => fakeList.ItemCount).WillReturn(10)

Please let me know if it helps.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Thanks, Elisha. I am gradually getting my head around the concept of mocking. I was approaching it as if TypeMock actually replaced SharePoint functionality. I wanted my fakeSPList object to behave like an SPList. But that was because I began my experimentation by fiddling with the TypeMock API rather than focusing on actually testing my code. I have learned that the way to do this is to examine the code under test and find the specific places that depend on SharePoint functionality. You then mock those specific bits. SharePoint applications do two basic things: they issue commands that change SharePoint's state, and they obtain information from SP about its state. The latter is mocked by faking return values, and is by far more important. I was trying to simulate the former -- but that turns out to be irrelevant. When my app changes SharePoint's state, it really doesn't matter whether SharePoint actually changes at all. My code only cares to the extent that it asks SharePoint about its state -- and that's what you mock.
answered by leighwebber (1.2k points)
0 votes
Hi,


A fix to the bug you found can be downloaded here. In any case, it will be available in our next version.

https://www.typemock.com/patches/TypemockIsolatorSuite6.2.3_splist_fix.msi
answered by yoel (1.9k points)
0 votes
Hi Leighwebber,

We would like to offer our assistance with using Isolator.

If you have any questions about how to test some code please feel free to ask us, here or via support@typemock.com.
answered by yoel (1.9k points)
...