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
I think the Isolated.WhenCalled On new Keyword should not Disturb Base Variable. Here's the production code:

    public abstract class BaseClass
    {

        public int Version
        {
            get
            {

                return 1;
            }

        }

        public string FixString
        {
            get;
            set;
        }

        public BaseClass()
        {
            FixString = "hello";
        }

        public virtual int GetBaseVersion()
        {
            return Version;
        }
    }


    public class DeriveClass: BaseClass
    {

        public new int Version
        {
            get
            {

                return 2;
            }

        }
    }


And here's the test code:
        [Test]
        public void CompareAllTheVersion()
        {
            DeriveClass cdes = new DeriveClass();
            Assert.AreEqual(2, cdes.Version);
            Assert.AreEqual(1, cdes.GetBaseVersion());
        }

        [Test]
        public void CompareAllTheVersion_IsolatedThing()
        {
            DeriveClass cdes = new DeriveClass();
            Isolate.WhenCalled(()=>cdes.Version)
                .WillReturn(3);
            Assert.AreEqual(3, cdes.Version);
            Assert.AreEqual(1, cdes.GetBaseVersion());
        }


The first test passes, but the second failed because cdes.GetBaseVersion() now also returns 3. But it should return 1, because

Isolate.WhenCalled(()=>cdes.Version)
                .WillReturn(3); 

should only affect the derived class one, not the base class one.
________
Asthma advice
asked by nsoonhui (59.1k points)

1 Answer

0 votes
Hi Soon Hui,

you're right, it seems like a bug. I've added it to our backlog.

Thanks!
answered by igal (5.7k points)
...