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
My test fixture includes this utility method to create a populated SPList:

private SPList MakeList()
        {
            SPList fakeList = Isolate.Fake.Instance<SPList>(Members.CallOriginal);
            for (int i = 1; i <= 6; i++)
            {
                Isolate.WhenCalled(() => fakeList.Items[i]).WillReturn(MakeTaskItem(i, "This is task number " + i.ToString()));
            }
            Isolate.WhenCalled(() => fakeList.GetItemById(1)).WillReturn(MakeTaskItem(1, "This is task number 1"));
            Isolate.WhenCalled(() => fakeList.GetItemById(2)).WillReturn(MakeTaskItem(2, "This is task number 2"));
            Isolate.WhenCalled(() => fakeList.GetItemById(3)).WillReturn(MakeTaskItem(3, "This is task number 3"));
            Isolate.WhenCalled(() => fakeList.GetItemById(4)).WillReturn(MakeTaskItem(4, "This is task number 4"));
            Isolate.WhenCalled(() => fakeList.GetItemById(5)).WillReturn(MakeTaskItem(5, "This is task number 5"));
            Isolate.WhenCalled(() => fakeList.GetItemById(6)).WillReturn(MakeTaskItem(6, "This is task number 6"));
            
            return fakeList;
        }


I have two tests:

[Test]
        public void FakeList_Items_3()
        {
            SPWeb spWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
            SPList spList = MakeList();
            SPListItem thisItem = spList.Items[3];
            Assert.That(thisItem.Title, Is.EqualTo("This is task number 3"));
        }

        [Test]
        public void FakeList_GetItemByID_3()
        {
            SPWeb spWeb = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
            SPList spList = MakeList();
            SPListItem thisItem = spList.GetItemById(3);
            Console.WriteLine(thisItem.Title);
            Assert.That(thisItem.Title, Is.EqualTo("This is task number 3"));
        }
       


The FakeList_Items_3 succeeds, but the FakeList_GetItemByID_3 fails. The thisItem object is always the first one, not the third. IOW, thisItem.Title is "This is task number 1", even though I asked for number 3.

How can I mock the GetItemById method? I use it all over the place in my actual code.
asked by leighwebber (1.2k points)

4 Answers

0 votes
Hi,

WhenCalled default behavior is to ignore the arguments and return the requested value. This is useful for the common usage where we want a method to always return a faked value without regarding the arguments.

In order to set the behavior for each argument, you can use WithExactArguments completing statement.

For example:
Isolate.WhenCalled(() => fakeList.GetItemById(1))
            .WithExactArguments()
            .WillReturn(MakeTaskItem(1, "This is task number 1"));


Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Perfect! Here's my revised code.
        private SPList MakeFakeList(int itemCount)
        {
            SPList fakeList = Isolate.Fake.Instance<SPList>();
            for (int i = 1; i <= itemCount; i++)
            {
                Isolate.WhenCalled(() => fakeList.Items[i]).WillReturn(MakeTaskItem(i, "This is task number " + i.ToString()));
                Isolate.WhenCalled(() => fakeList.GetItemById(i)).WithExactArguments().WillReturn(MakeTaskItem(i, "This is task number " + i.ToString()));
            }
            return fakeList;


Interesting, though. When I try this:
        private SPList MakeFakeList(int itemCount)
        {
            SPList fakeList = Isolate.Fake.Instance<SPList>();
            for (int i = 1; i <= itemCount; i++)
            {
                Isolate.WhenCalled(() => fakeList.Items[i]).WithExactArguments().WillReturn(MakeTaskItem(i, "This is task number " + i.ToString()));
                Isolate.WhenCalled(() => fakeList.GetItemById(i)).WithExactArguments().WillReturn(MakeTaskItem(i, "This is task number " + i.ToString()));
            }
            return fakeList;
        }


... I get a run-time error on the first .WhenCalled: Object reference not set to an instance of an object. Looks like it is trying to obtain an actual Item[i] before it defines the .WillReturn. It's a bit confusing -- but I'm a newb.
answered by leighwebber (1.2k points)
0 votes
Hi,

It seems like a bug. I'll try to reproduce it here.

Can you please also post the stack trace of the exception and let me know what version of Isoaltor and version of .NET are you using?

Thanks,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Here's my utility method:
       private SPList MakeFakeList(int itemCount)
        {
            SPList fakeList = Isolate.Fake.Instance<SPList>();
            for (int i = 1; i <= itemCount; i++)
            {
                Isolate.WhenCalled(() => fakeList.Items[i])
                    .WithExactArguments()
                    .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;
        }

Here's the test:
        [Test]
        public void FakeList_GetItemByID_8()
        {
            SPList spList = MakeFakeList(10);
            SPListItem thisItem = spList.GetItemById(8);
            Assert.That(thisItem.Title, Is.EqualTo("This is task number 8"));
        }

And here's the nUnit error:
LSMSTests.Tests.FakeList_GetItemByID_8:
System.NullReferenceException : Object reference not set to an instance of an object.
at iw.a(gs A_0, Int32 A_1, Object A_2)
at iw.c.b()
at iw.a(Object A_0, Boolean A_1, Func`1 A_2, Action A_3, Action A_4, Action A_5)
at iw.b(Object A_0)
at TypeMock.ArrangeActAssert.ExpectationEngine`1.a(TResult A_0)
at LSMSTests.Tests.MakeFakeList(Int32 itemCount) in D:SourceLSMSLSMSSupervisorLSMSTestsTests.cs:line 236
at LSMSTests.Tests.FakeList_GetItemByID_8() in D:SourceLSMSLSMSSupervisorLSMSTestsTests.cs:line 81
answered by leighwebber (1.2k points)
...