I have the following test code:
new PersistenceSpecification<Client>(Session, new CustomEqualityComparer())
.CheckProperty(c => c.Name, "Client")
.CheckList(c => c.Contracts, new HashSet<Contract> { new Contract(DateTime.UtcNow, "Name", "Detail", "By", DateTime.UtcNow, "RegisteredName", "Reg#", new ContractType("Type"), EscalationType.AnnualPercentageAllProducts, ContractDuration.Custom) })
.CheckList(c => c.UserAliases, new HashSet<ClientUserAlias> { new ClientUserAlias() })
.CheckList(c => c.Industries, new List<ClientIndustry> { new ClientIndustry(client, Guid.NewGuid()) })
.CheckList(c => c.Addresses, new HashSet<ClientAddress> { new ClientAddress(client, physicalAddress, AddressType.Physical), new ClientAddress(client, postalAddress, AddressType.Postal) })
.VerifyTheMappings(client);
The test sometimes passes and at other times it fails. I have tracked it down to the CheckList on c.Addresses. There are two different ClientAddresses and they are not always compared in the same order. So my CustomEqualityComparer sometimes compares the postal with the physical address and fails.
Here is my comparer:
public class CustomEqualityComparer : IEqualityComparer
{
public bool Equals(object x, object y)
{
if (x == null || y == null)
return false;
if (x is Entity && y is Entity)
return ((Entity)x).Id == ((Entity)y).Id;
if (x is IntEntity && y is IntEntity)
return ((IntEntity)x).Id == ((IntEntity)y).Id;
return x.Equals(y);
}
public int GetHashCode(object obj)
{
throw new NotImplementedException();
}
}
How do I maintain the order for the CheckList comparison?
Aucun commentaire:
Enregistrer un commentaire