Unit Test Assert.AreEqual failed

后端 未结 6 1360
别那么骄傲
别那么骄傲 2021-01-17 16:12

I have a unit test for a method which gets an object from a collection. This keeps failing and I cannot see why, so I have created a very simple test below to create 2 suppl

6条回答
  •  囚心锁ツ
    2021-01-17 16:30

    The default implementation of Object.Equals for reference types (ie. classes) is "Reference Equality": are the two objects actually the same instance. It doesn't compare the values of fields.

    Either (as others have shown) override Equals to give "Value Equality". In this caseyou must also override GetHashCode (so containers work), and should override operator ==.

    Alternatively accept that most entities should have reference equality (two suppliers with the same name are not always the same organisation) and actually use the properties directly.

提交回复
热议问题