How to set up a recursive call on a faked object?

0 votes

I have a case where the code under test, product1, calls into a mocked object, mock1.Entry, where mock1 calls back to product1 which calls back into mock1.Entry.  The second call to mock1.Entry does not get mocked.  Is there some way to continue to mock this second call?

A simplified example:

    [TestClass]
    public class UnitTest1
    {
        public class C1
        {
            public void Foo(int val) { }
        }
        public class C2
        {
            private C1 _c1;
            public C2(C1 c1) { _c1 = c1; }
            public void FooCaller(int val)
            {
                if (val < 1) { _c1.Foo(val); }
            }
        }
        [TestMethod]
        public void TestMethod1()
        {
            C1 c1Fake = Isolate.Fake.Instance<C1>();
            C2 target = new C2(c1Fake);
            Isolate.WhenCalled(() => c1Fake.Foo(0)).DoInstead(
                context =>
                {
                    int passedVal = (int)context.Parameters[0];
                    target.FooCaller(passedVal + 1);

                });
            target.FooCaller(-1);
        }
}

I expected:

target.FooCaller->c1Fake.FooCaller(DoInstead)->target.FooCaller->c1Fake.FooCaller(DoInstead)

instead, I got:

target.FooCaller->c1Fake.FooCaller(DoInstead)->target.FooCaller->c1Fake.FooCaller(ACTUAL METHOD)

This behavior used to work as expected in 6.0.8 and has changed in the 8.1.1.11 version that I'm now using.

Thanks,

-Kevin

asked Apr 11, 2016 by kevinms99 (1,140 points)
I'm hoping to get a response to this issue.  It's now been 10 days without any response.

3 Answers

0 votes

Hi Kevin!

We reproduced this issue on our machines, so i can confirm that this is bug. 

I'll update here on the status.

answered Apr 25, 2016 by eva (1,580 points)
0 votes
Hi, Kevin.

Faking inside DoInstead() block was limited. We changed it, by adding a special flag for indicate recursion.

You'll see it in our release very soon.
answered Jun 7, 2016 by eva (1,580 points)
0 votes

This issue is fixed in version 8.4.0 using the new API 

MethodCallContext.ContinueFakingThisMethodRecursively

 

answered Jun 14, 2016 by eva (1,580 points)
...