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
0 votes
    public class RealLogger
    {
        public int id;

        public RealLogger()
            : this(-1)
        {
        }

        public RealLogger(int id)
        {
            this.id = id;
        }
    }


        [TestMethod]
        [Isolated]
        public void FutureInstance_VerifyMethodWasCalledAndStubbed()
        {
            var fake = Isolate.Fake.Instance<RealLogger>();
            Isolate.Swap.NextInstance<RealLogger>().With(fake);
            var real = new RealLogger(77);
            Trace.TraceInformation("1: fake.id={0}, real.id={1}", fake.id, real.id);
            Isolate.CleanUp();

            fake = Isolate.Fake.Instance<RealLogger>();
            Isolate.Swap.NextInstance<RealLogger>().With(fake);
            fake.id = 8;
            real = new RealLogger(77);
            Trace.TraceInformation("2: fake.id={0}, real.id={1}", fake.id, real.id);
            Isolate.CleanUp();


            fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal);
            Isolate.Swap.NextInstance<RealLogger>().With(fake);
            real = new RealLogger(77);
            Trace.TraceInformation("3: fake.id={0}, real.id={1}", fake.id, real.id);
            Isolate.CleanUp();

            fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal);
            Isolate.Swap.NextInstance<RealLogger>().With(fake);
            fake.id = 8;
            real = new RealLogger(77);
            Trace.TraceInformation("4: fake.id={0}, real.id={1}", fake.id, real.id);
            Isolate.CleanUp();

            fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal, ConstructorWillBe.Called, 3);
            Isolate.Swap.NextInstance<RealLogger>().With(fake);
            real = new RealLogger(77);
            Trace.TraceInformation("5: fake.id={0}, real.id={1}", fake.id, real.id);
            Isolate.CleanUp();

            fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal, ConstructorWillBe.Called, 3);
            Isolate.Swap.NextInstance<RealLogger>().With(fake);
            fake.id = 8;
            real = new RealLogger(77);
            Trace.TraceInformation("6: fake.id={0}, real.id={1}", fake.id, real.id);
            Isolate.CleanUp();
        }


Output:
QTAgent32.exe Information: 0 : 1: fake.id=0, real.id=0
QTAgent32.exe Information: 0 : 2: fake.id=8, real.id=0
QTAgent32.exe Information: 0 : 3: fake.id=-1, real.id=77
QTAgent32.exe Information: 0 : 4: fake.id=8, real.id=77
QTAgent32.exe Information: 0 : 5: fake.id=3, real.id=77
QTAgent32.exe Information: 0 : 6: fake.id=8, real.id=77


Who can explain this?
What I'm doing wrong?

Thanks a lot.
asked by Mocker (600 points)

1 Answer

0 votes
Hi,
There are few issues here and I'll try to answer them the best I can.
First the word "Swap" is kind misleading here.
The Isolator does not really swap the instances, what really happens is that Isolator takes the expectations that you put on the faked instance and apply it to the new (real) instance.
Second the Isolator works by intercepting methods and changing their behavior at runtime, which means it has no effect on fields.

This explains why when setting the value of id field on the fake instance has no effect on the real instance.
Note that if you convert id to property it will work! i.e setting the value of property on a fake instance will change the return value of the real.

On case #5 in your example you stumbled upon an age case.
When you write:
fake = Isolate.Fake.Instance<RealLogger>(Members.CallOriginal, ConstructorWillBe.Called, 3);
You probably mean - "call the real constructor but replace the argument to 3 instead of the original argument". Sadly this does not happen and the Isolator let the real constructor run without changing its argument. This seems like a bug :(

Hope I answered your questions.
Please let me know if there are still some unclear issues regarding swapping.
answered by ohad (35.4k points)
...