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'm not sure whether this is a bug. I have the following production code:

    public class NothingClass
    {
        public int Gradient
        { get; set; }

        public override bool Equals(object obj)
        {

            var nothingOther = (NothingClass) obj;
            return Gradient == nothingOther.Gradient;
        }

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


And here's the test code:
        [Test]
        public void ToListTest()
        {
            var sw1 = new NothingClass();

            CompareArrayList(sw1);

            var sw2 = Isolate.Fake.Instance<NothingClass>();
        
            CompareArrayList(sw2);
        }

        private static void CompareArrayList<T>(T sw1)
        {
            var swList = new List<T> {sw1};

            var swArray = new[] {sw1};
            Assert.AreEqual(swList[0], swArray.ToList()[0]);
        }


This test will fail. But if I remove the Equals and GetHashCode in GetNothing, then it will pass.

Is this a bug?
________
Granny vid
asked by nsoonhui (59.1k points)

1 Answer

0 votes
Hi Soon Hui,

This happens because you override the Equals() method, and define a recursive fake. Recursive fakes will automatically stub out all methods on the type, which means the Equals() method will return false by default, and your assert will fail. You can work around this by setting a CallOriginal behavior on the Equals() method.

Doron
Typemock Support
answered by doron (17.2k points)
...