Unit testing and objects equality

February 16th, 2009

Let's assume that we have a class like below:

C#:
  1. public class Foo : IEquatable<Foo>
  2.     {
  3.         private int variable;
  4.  
  5.         public int Variable
  6.         {
  7.             get { return variable; }
  8.             set { variable = value; }
  9.         }
  10.  
  11.         #region IEquatable<Foo> Members
  12.  
  13.         public bool Equals(Foo other)
  14.         {
  15.             return this.variable == other.Variable;
  16.         }
  17.  
  18.         #endregion
  19.     }

and a unit test for that:

C#:
  1. [TestClass()]
  2.     public class FooTest
  3.     {
  4.         [TestMethod()]
  5.         public void EqualsTest()
  6.         {
  7.             Foo one = new Foo();
  8.             one.Variable = 5;
  9.             Foo two = new Foo();
  10.             two.Variable = 5;
  11.  
  12.             Assert.AreEqual<Foo>(one, two, "AreEqual<Foo> failed");
  13.             Assert.AreEqual(one, two, "AreEqual failed");
  14.             Assert.AreSame(one, two, "AreSame failed");
  15.         }
  16.     }

and a question is - which Assert fails? All of them o_O
I could not find an explanation why implemented Equals method is not called. Framework always calls default Equals(object)

Ballet of PES

February 8th, 2009

My PES replays - Enjoy ;)

Designed by SirMike © All rights reserved

Valid XHTML 1.0! Valid CSS!

Powered by Rootnode