public void CloneTests2() {
//Make an entity with validation errors
var firstEntity = new MyEntity();
firstEntity.EntityAspect.ValidationErrors.Add(new VerifierResult(false));
//Make a copy of the original entity
var secondEntity = (MyEntity)((ICloneable)firstEntity).Clone();
//The validations errors will correctly get copied to the cloned entity.
Assert.AreEqual(1, secondEntity.EntityAspect.ValidationErrors.Count);//Pass
//But the VerifierErrorsCollection on the cloned entity isn't quite setup right. For example...
//See if ErrorsChanged fires on the second entity
var errorChangeFiredOnSecondEntity = false;
((INotifyDataErrorInfo)secondEntity).ErrorsChanged += (_, __) => errorChangeFiredOnSecondEntity = true;
//Clear out the errors on the second entity to see what happens
secondEntity.EntityAspect.ValidationErrors.Clear();
//The error was correctly removed
Assert.AreEqual(0, secondEntity.EntityAspect.ValidationErrors.Count);//Pass
//However, this line will fail because ErrorsChanged was not raised on the second entity. In fact, it was raised on the
// first entity - even though the errors on the first entity weren't touched.
Assert.IsTrue(errorChangeFiredOnSecondEntity);//Fail
//Further support for the VerifierErrorsCollection being messed up....let's check out the _entityAspect field on the collection
//On the first entity, the aspect is correct
Assert.AreSame(firstEntity.EntityAspect, GetAspect(firstEntity.EntityAspect.ValidationErrors));//Pass
//However, these two lines will fail. We'd expect the aspect of the second collection to be the same as the second
// entity's aspect...but it's not! And in fact, the aspect is still pointing to the first entity
Assert.AreSame(secondEntity.EntityAspect, GetAspect(secondEntity.EntityAspect.ValidationErrors));//Fail
Assert.AreNotSame(firstEntity.EntityAspect, GetAspect(secondEntity.EntityAspect.ValidationErrors));//Fail
}
private EntityAspect GetAspect(EntityAspect.VerifierErrorsCollection collection)
{
//Ugly code to get at the private field
return (EntityAspect)typeof(EntityAspect.VerifierErrorsCollection)
.GetField("_entityAspect", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(collection);
}