Customer actual = new Customer { Id = 1, Name = "John" }; Customer expected = new Customer { Id = 1, Name = "John" }; Assert.Equal(expected, actual); // The test will fail here
However, if you change the assert to be based on each property, they will be equal and your test will pass:
Assert.Equal(expected.Id, actual.Id); Assert.Equal(expected.Name, actual.Name);
The first example fails due to the way comparison works for reference types. By default, the equality operation for those types will only assert whether the two objects being compared are the same, namely your variables are pointing to the same object within the memory heap. Due to this, out of the box, both .Equal and .Same assertions will always result in the same outcome and will only be positive in cases such as this:
Customer john = new Customer { Id = 1, Name = "John" }; Customer john2 = john;
Assert.Equal(john, john2); Assert.Same(john, john2);
On the other hand, the second example in which we were asserting the properties inside Customer, worked. The property Id is of type System.Int32, a value type, so its comparison will actually check whether the objects have the same value. The property Name is of type System.String, which although is a reference type, it behaves like value types for comparison purposes