I am currently trying to compare two lists, with the same items in it, with xUnit but getting an error while running.
Assert.Equal(expectedList, actualList);
Error:
"Assert.Equal() Failure"
Expected: List<myObject> [myObject { modifier = '+', name = "name", type = "string" }, myObject { modifier = '+', name = "age", type = "int" }]
Actual: List<myObject> [myObject { modifier = '+', name = "name", type = "string" }, myObject { modifier = '+', name = "age", type = "int" }]
This has to do with object equality.
MyObject
does not implement theEquals method
. By default you get a reference equality. I assume you have two different objects forMyObject
.Meaning it does not matter that your List holds the similar object(meaning with same values) they are not of the same reference, so your test checks that, this is why it fails.
When we update our class to this.
Also as mentioned in the comments by Jonathon Chase.
It is a good idea to
override
theGetHashCode()
method as well. It is preferred to inherit fromIEquatable<T>
so you can avoid casting.Everything goes green.