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
This is a hard one: ReturnRecursiveFakes doesn't handle override equal correctly

Here's the production code:
    public class SimpleClass
    {
        public string ID
        {
            get;
            set;
        }

        public string Node
        {

            get
            {
                throw new NotImplementedException();
            }
        }

        public override bool Equals(object obj)
        {
            return ID == ((SimpleClass) obj).ID;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public void MOreVerify(int moerVer)
        {
            
        }
    }


Here's the test code:
        [Test, Isolated]
        public void NodeOverrideTest2()
        {
            List<SimpleClass> scs = new List<SimpleClass>();
            for (int l = 0; l < 2; l++)
            {

                SimpleClass sc = Isolate.Fake.Instance<SimpleClass>(Members.ReturnRecursiveFakes);
                Isolate.WhenCalled(() => sc.ID).WillReturn("1");
                scs.Add(sc);
            }

            scs.Remove(scs[0]);
            Assert.AreEqual(1, scs.Count);       
        }


And the test will fail.

BUt if I use
         [Test, Isolated]
        public void NodeOverrideTest2()
        {
            List<SimpleClass> scs = new List<SimpleClass>();
            for (int l = 0; l <2> sc.ID).WillReturn("1");
                scs.Add(sc);
            }

            scs.Remove(scs[0]);
            Assert.AreEqual(1, scs.Count);       
        }


Then the test will pass.

I think it's because the ReturnRecursiveFakes just return a false for equals but fail to take into account that the equals should be true under certain circumstances.
________
BMW VI 5,5
asked by nsoonhui (59.1k points)

2 Answers

0 votes
If I test
Assert.AreEqual(scs[0], scs[1]);


The test will also fail. If I modify the test to
        [Test, Isolated]
        public void NodeOverrideTest2()
        {
            List<SimpleClass> scs = new List<SimpleClass>();
            for (int l = 0; l < 2; l++)
            {

                SimpleClass sc = Isolate.Fake.Instance<SimpleClass>(Members.ReturnRecursiveFakes);
                Isolate.WhenCalled(()=>sc.Equals(null)).CallOriginal();
                Isolate.WhenCalled(() => sc.ID).WillReturn("1");
                scs.Add(sc);
            }
            Assert.AreEqual(scs[0], scs[1]);
            scs.Remove(scs[0]);
            Assert.AreEqual(1, scs.Count);       
        }


Then this test will pass.

Any idea on how to improve the current situation?
________
buy vaporgenie
answered by nsoonhui (59.1k points)
0 votes
Hi Soon Hui,

The behavior you're showing happens because when you're faking SomeClass using "Recursive Fakes" you fake Equals and GateHashCode functions as well.
In fact the following call shall return false:
 bool result = sc.Equals(sc);

In order to call the actual function you'll need to use CallOriginal for the specific call:
Isolate.WhenCalled(() => sc.Equals).CallOriginal();

or create the fake with Member.CallOriginal instead
answered by dhelper (11.9k points)
...