Mark
Mark

Reputation: 11

How do I correctly work with C# automated test AreEqual?

I'm workin on some automated test for a C# project but I'm having some problem to make the test pass when it should on big classes. All of the test that cover the specific parameters passes but the main one does not. If I write a custom MainMemento Equals method and manually put every parameter in it the test pass but it's not an elegant solution.

[Test]
public void TestMainMementoComparison() {
    Tuple<MainMemento, MainMemento> data = SaveLoad();
    Assert.AreEqual(data.Item1, data.Item2);
}
[Serializable]
public class MainMemento {
    public StateMemento StateMap { get; set; }
    public DepartmentEffectsMemento DepartmentEffects { get; set; }
    public GeneralDatabaseMemento<DocumentInstanceID, DocumentInstance> DocumentDB { get; set; }
    public GeneralDatabaseMemento<BuildingID, Building> BuildingDB { get; set; }
    public GeneralDatabaseMemento<CityActionID, CityAction> CityActionDB { get; set; }
    public int PlayerWeekToSkip { get; set; }
    public DocumentsHandlerMemento DocumentsHandler { get; set; }
    public List<KeyValuePair<GameLaws.Law, LawStatus>> Laws { get; set; }
    public Boolean SimulationState { get; set; }
    public Session.Settings Settings { get; set; }
    public ElementsPoolMemento<SaveableSprite> portraitPool { get; set; }
    public override bool Equals(object obj) {
        if (obj == null || GetType() != obj.GetType()) {
            return false;
        }
        MainMemento other = (MainMemento)obj;
        if (!StateMap.Equals(other.StateMap))
            return false;
        if (!DepartmentEffects.Equals(other.DepartmentEffects))
            return false;
        if (!BuildingDB.Equals(other.BuildingDB))
            return false;
        if (!CityActionDB.Equals(other.CityActionDB))
            return false;
        if (!PlayerWeekToSkip.Equals(other.PlayerWeekToSkip))
            return false;
        if (!DocumentsHandler.Equals(other.DocumentsHandler))
            return false;
        if (!GameUtils.AreEqual<KeyValuePair<GameLaws.Law, LawStatus>>(Laws, other.Laws))
            return false;
        if (!SimulationState.Equals(other.SimulationState))
            return false;
        if (!Settings.Equals(other.Settings))
            return false;
        if (!portraitPool.Equals(other.portraitPool))
            return false;
        return true;
    }
    public override int GetHashCode() { return base.GetHashCode(); }
}

Upvotes: 1

Views: 48

Answers (2)

shatulsky
shatulsky

Reputation: 336

You may use fluentassertions package to compare.

It allows you to compare objects without Equals overriding.

See this article

Upvotes: 0

impartial function
impartial function

Reputation: 41

It may not be elegant but it is by language design that for classes you have to implement the Equals override like this. You can consider using a struct or a record instead, which auto-implement value comparison for Equals. Do watch out, however, whether your properties' types support it. For example, if StateMemento is a reference type, you may still not get what you want, unless you also change these types.

Since your class is already serializable, you could compare the serialized strings, though.

Upvotes: 1

Related Questions