I am writing Unit Tests and thinking about the scenario of a type that implements IClonable
.
So of course I want to have a Unit Test that tests the Clone()
method.
[Test]
public void CloneObject()
{
MyType original = new MyType();
MyType clone = (MyType)original.Clone();
// Assert for equality
}
So my first task it to have an Assert
for the equality. I see following options:
- going through all properties (fields) of
MyType
and check them one by one - override
Equals()
inMyType
to letMyType
say if two instances are equal (consider that sometimes equality for tests is considered different of equality for production code) - Check with some kind of serialization if the instances are equal (for that scenario
MyType
would have to be[Serializable]
, but that sometimes is hard to do if it has e.g. Interface Properties) - ...??
For the first two I can setup my tests and they work well. But what if I change MyType
and add an additional property? If Clone()
doesn't copy this and I don't add it in the list of checked properties or the equals method my test still passes even if the property does not get copied.
How do you solve this kind of tests?