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
Hi, in the following I am trying to fake a method - but call the real instance method as well.
For some reason, it appears that the fake method is called twice even though I only invoke the method once - why is this?

    
class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Test();
    }

    private static int doInsteadCalledCount = 0;
    public void Test()
    {
        MyClass mc = Isolate.Fake.Instance<MyClass>(Members.CallOriginal);
        Isolate.WhenCalled(() => mc.MyMethod(null)).
            DoInstead((callContext) =>
            {
                doInsteadCalledCount++;

                // Call the real instance method:
                MyParam p = callContext.Parameters[0] as MyParam;
                MyClass real = callContext.Instance as MyClass;

                MyResult result = real.MyMethod(p);
                return result;
            });
        Isolate.Swap.AllInstances<MyClass>().With(mc);

        MyClass myClass = new MyClass();
        MyParam myParam = new MyParam() { Value = "parameter value" };

        // Call "MyMethod". The fake method above gets called twice (?)
        MyResult myResult = myClass.MyMethod(myParam);
        MyResult myResult2 = myClass.MyOtherMethod(myParam);

        int count = doInsteadCalledCount;
    }

    private class MyClass
    {
        public MyResult MyMethod(MyParam p)
        {
            return new MyResult() { ResultValue = 1 };
        }
        public MyResult MyOtherMethod(MyParam p)
        {
            return new MyResult() { ResultValue = 2 };
        }
    }

    private class MyParam
    {
        public string Value
        {
            get; set;
        }
    }

    private class MyResult
    {
        public int ResultValue
        {
            get; set;
        }
    }
}


Thanks, Peter
asked by xdzgor (3.3k points)

1 Answer

0 votes
Hi Peter,

For explanation why your callback method is called twice see my answer to your previous post :)

If you want to fake only the first call of the method you can set several behavior with sequencing:
// production code
public class MyClass
{
    public int Bar(string s)
    {
        return 1;
    }
}

// test code
[Test]
public void Test()
{
    var fake = Isolate.Fake.Instance<MyClass>();
    Isolate.WhenCalled(() => fake.Bar(null)).WillReturn(5);
    Isolate.WhenCalled(() => fake.Bar(null)).CallOriginal();

    Assert.AreEqual(5, fake.Bar(null));
    Assert.AreEqual(1, fake.Bar(null));
    Assert.AreEqual(1, fake.Bar(null));
}


In the example above you can see when set several behaviors for a method the Isolator will apply them to the method in the order that you set in the test.
The last behavior will become the default once all expectations has been run.
In this case it means that after the first call has been faked all calls to fake.Bar() will not be faked.

Please let me know if it helps.
answered by ohad (35.4k points)
...