locked
Assert.AreEqual fails for derived class RRS feed

  • Question

  • Using VS2012 Pro RC.

    I have a base class Client with 2 instances client1 and client2.

    Class User is derived from Client . The User class has a couple of additional attributes, a1, a2. I have 2 instances of the User class user1 and user2.

    Assert.AreEqual (class1, class2) passes.

    Assert.AreEqual (user1, user2) fails

    Assert.AreEqual (user1.a1, user2.a1) passes

    Assert.AreEqual (user1.a2, user2.a2) passes

    How could it be that the assertion passes on the base class and all attributes of the derived class yet fails on the derived class?


    Jen

    Saturday, October 20, 2012 11:09 PM

Answers

  • Hi Jen,

    Thanks for using VS2012 and posting your query on MSDN.

    Assert.AreEqual uses <class>.Equals(object) for equality check. By default if Equals() is not overridden in the class/base class then object.Equals() is used which checked equality by reference (Refer http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx).

    Assert.AreEqual(user1, user2) fails is an expected behavior as class don't have Equals() overridden hence equality check happens on object reference and fails.

    Override Equals() function in user class as follows and Equality of user1 and user2 should pass.

        public override bool Equals(object obj)
        {
            User anotherUser = obj as User;
            if (anotherUser == null)
            {
                return false;
            }
    
            return anotherUser.a1 == this.a1 && anotherUser.b1 == this.b1;
        }

    I can't explain equality of the base class, as I am getting client1 & client2 as not equal on my VS 2012. If you can share complete code then might able to help you understand why its evaluating equal.


    Regards,
    Vikram Agrawal,
    Developer, VSTLM, Microsoft Corporation

    • Marked as answer by Amanda Zhu Wednesday, October 24, 2012 12:38 AM
    Monday, October 22, 2012 4:28 AM

All replies

  • Hi Jen,

    Thanks for using VS2012 and posting your query on MSDN.

    Assert.AreEqual uses <class>.Equals(object) for equality check. By default if Equals() is not overridden in the class/base class then object.Equals() is used which checked equality by reference (Refer http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx).

    Assert.AreEqual(user1, user2) fails is an expected behavior as class don't have Equals() overridden hence equality check happens on object reference and fails.

    Override Equals() function in user class as follows and Equality of user1 and user2 should pass.

        public override bool Equals(object obj)
        {
            User anotherUser = obj as User;
            if (anotherUser == null)
            {
                return false;
            }
    
            return anotherUser.a1 == this.a1 && anotherUser.b1 == this.b1;
        }

    I can't explain equality of the base class, as I am getting client1 & client2 as not equal on my VS 2012. If you can share complete code then might able to help you understand why its evaluating equal.


    Regards,
    Vikram Agrawal,
    Developer, VSTLM, Microsoft Corporation

    • Marked as answer by Amanda Zhu Wednesday, October 24, 2012 12:38 AM
    Monday, October 22, 2012 4:28 AM
  • Thank you for your prompt response Vikram. My apologies for the delay in getting back to you ... things got a bit hectic here in the last few days. I shall be trying what you suggested today and will get back to you.

    Thanks again


    Jen


    • Edited by FSL AU Wednesday, October 24, 2012 3:30 AM
    Wednesday, October 24, 2012 12:52 AM
  • Thank you Vikram; worked a treat!

    Jen

    Wednesday, October 24, 2012 3:29 AM