Thank you for your answers, they helped me in the future, but none of them helped me completely.
If I override the Equals and GetHashCode methods of the object as follows:
public override bool Equals(object obj) { return true; } public override int GetHashCode() { return 0; }
methods are called and my tests will succeed. However, of course, I do not want to implement these methods for the parameters of an object of type. Why does my class offer to compare instances only with any instance of an object?
So, I modified this method as static: public **static** bool Equals(Subject x, Subject y) and introduced a new test method (all tests in one method for short):
[TestMethod] public void SubjectTestWithSubjectEquals() { Assert.IsTrue(Subject.Equals(new Subject(1), new Subject(1))); Assert.IsTrue(new Subject(1).Equals(new Subject(1))); Assert.IsFalse(Subject.Equals(new Subject(1), new Subject(2))); Assert.IsFalse(new Subject(1).Equals(new Subject(2))); }
These tests will succeed. However, I'm not sure if this is a good way. Firstly, for success Assert.IsTrue((new Subject(1)) == (new Subject(1))); I needed to implement:
public static bool operator ==(Subject x, Subject y) { return Equals(x, y); }
and it also causes:
public static bool operator !=(Subject x, Subject y) { return !Equals(x, y); }
In addition, taking another step, wanting to be able to compare lists of type Subject, I tried this test:
[TestMethod] public void SubjectTestWithCollections() { var list1 = new List<Subject>() { new Subject(1), new Subject(2) }; var list2 = new List<Subject>() { new Subject(1), new Subject(2) }; CollectionAssert.AreEqual(list1, list2); }
this will not work, since, apparently, each element will be compared using Assert.AreEqual, not Subject.Equals. I saw problems with CollectionAssert.AreEquivalent , which makes me wonder if this is mainly due to the way Microsoft.VisualStudio.TestTools.UnitTesting is implemented, or that I shouldn’t try to fuss with peers in general.