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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;

namespace TypeMockTests
{
    [TestClass]
    public class TestInterfaceMockInDoInstead
    {
        [TestMethod, Isolated]
        public void TestFailsWhenIsolatedInDoInstead()
        {
            ISomeObject obj = Isolate.Fake.Instance<ISomeObject>();
            ISomeObject parentObj = Isolate.Fake.Instance<ISomeObject>();
            Isolate.WhenCalled(() => obj.Parent).WillReturn(parentObj);

            SomeClass someClass = Isolate.Fake.Instance<SomeClass>();

            // check that the Isolated behavior is working correctly
            Assert.AreEqual(parentObj, obj.Parent);

            Isolate.WhenCalled(() => someClass.DoSomething(null)).DoInstead(
                x =>
                {
                    // at this point, the Isolated behavior is lost for some reason
                    Assert.AreEqual(parentObj, obj.Parent);
                });

            someClass.DoSomething(obj);
        }

        public interface ISomeObject
        {
            ISomeObject Parent { get; }
        }

        public class SomeClass
        {
            public void DoSomething(ISomeObject obj)
            {
            }
        }
    }
}


The first assertion passes, but the Isolated behavior is lost in the DoInstead block so the second assertion fails.
asked by Christian.Merat (2.9k points)

4 Answers

0 votes
Hi,

This behavior you see is "by design". Inside the DoInstead block no faking is done, therefor the behavior set using WhenCalled before is not used and the call is routed to the original implementation.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
edit: removed
answered by filipham (180 points)
0 votes
As a follow-up to this, there seems to be a bug in the DoInstead behavior as you've described it above: it appears that, in the case where the DoInstead calls the same method on the same object (which should not be mocked since it's occurring inside the DoInstead block), the DoInstead handler is called a second time.

Code to reproduce:

[TestClass]
public class TestDoInstead
{
   [TestMethod, Isolated]
   public void Test()
   {
      // ARRANGE
      int numCalls = 0;
      Isolate.WhenCalled(() => this.GetBool()).DoInstead(
         x =>
         {
            numCalls++;

            return this.GetBool();
         });

      // ACT
      this.GetBool();

      // ASSERT
      Assert.AreEqual(1, numCalls);
   }

   private bool GetBool()
   {
      return true;
   }
}


We'd expect numCalls to only be 1, but it's actually 2.
answered by Christian.Merat (2.9k points)
0 votes
Hi,

Thanks for the repro, it seems like a bug. We'll investigate it and update on this thread.

Thanks,
Elisha,
Typemock Support
answered by Elisha (12k points)
...