I'm writing a unit test for some .net code I've written.
I'm familiar with writing code like this:
int expected = 10;
int actual = multiplyByTwo(5);
Assert.AreEqual(expected, actual);
In the case that the arguments are integers, it's clear what to expect the code to do.
What does the code do when the arguments passed in are objects?
If I've written a custom classed called MyClass
, how can I control when Assert.AreEqual
passes and failed with objects of type MyClass
?
The official documentation is pretty laconical and doesn't explain it, so I believe that if the objects are not primitives, then their references will be compared.
That is, two references to the same object would evaluate as being equal; two clones of a same object would evaluate as being different. Unless you overload the Equals()
instance method of the class(es) those objects belong to, or the ==
operator for said class(es).
Also see Reed Copsey's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With