MSUnit:Assert.AreEqual使树失败

时间:2011-08-19 19:55:32

标签: c# unit-testing tdd

我必须测试树的相等性。换句话说,包含List<T>的子对象和子对象的对象也包含带有子对象的List<T>,依此类推。

我发现你可以使用CollectionAssert测试List,但是它对复合材料的效果不佳。

有什么建议吗? MSUnit是我的测试库。

实施例

IReagentComposed bronzeBarParsed = (from n in composedCrafts where n.ItemId == 2841 select n).Single();

IReagentComposed bronzeBar = new Craft()
{
    ItemId = 2841,
    Profession = Profession.Mining,
    Quantity = 0,
    QuantityCrafted = 0,
    Skill = 50,
    Reagents = new List()
    {
        new Craft()
        {
            ItemId = 2840,
            Quantity = 0,
            Skill = 1,
            Profession = Profession.Mining,
            Reagents = new List()
            {
                new Reagent()
                {
                    ItemId = 2770,
                    Quantity = 1
                }
            }
        },
        new Craft()
        {
            ItemId = 3576,
            Quantity = 0,
            Skill = 50,
            Profession = Profession.Mining,
            Reagents = new List()
            {
                new Reagent()
                {
                    ItemId = 2771,
                    Quantity = 1
                }
            }
        }
    }
};

Assert.AreEqual(bronzeBar, bronzeBarParsed);

工艺和试剂

public class Craft : IReagentComposed
{
    public int QuantityCrafted { get; set; }
    public int Quantity { get; set;}
    public int ItemId { get; set; }
    public int Skill { get; set; }
    public Profession Profession { get; set; }
    public IEnumerable Reagents { get; set; }


    public override bool Equals(object other)
    {
        if (other == null || GetType() != other.GetType()) return false;

        IReagentComposed o = other as IReagentComposed;

        return o != null && this.Quantity == o.Quantity &&
                this.ItemId == o.ItemId &&
                this.Profession == o.Profession &&
                this.Reagents == o.Reagents && //also tried Equals
                this.Skill == o.Skill;
    }

    public override int GetHashCode()
    {
        return 0;
    }
}

public class Reagent : IReagent
{
    public int ItemId { get; set; }
    public int Quantity { get; set; }

    public override bool Equals(object other)
    {
        if (other == null || GetType() != other.GetType()) return false;

        IReagent o = other as IReagent;

        return o != null && o.ItemId == this.ItemId && o.Quantity == this.Quantity;
    }

    public override int GetHashCode()
    {
        return 0;
    }
}

2 个答案:

答案 0 :(得分:2)

        return o != null && this.Quantity == o.Quantity &&
                this.ItemId == o.ItemId &&
                this.Profession == o.Profession &&
                this.Reagents == o.Reagents && //also tried Equals
                this.Skill == o.Skill;

试剂不太可能匹配,它们是不同的List对象。列表与LT;&GT;虽然不清楚你使用的实际类型,但不会覆盖Equals。编写一个小辅助函数,它需要两个试剂列表并检查是否相等。您通常从比较List&lt;&gt; .Count开始,然后逐个处理元素。

答案 1 :(得分:0)

IEnumberable<T>添加了一种扩展方法:

public static class IEnumberableExtensions
{
    public static bool AreEnumerablesEqual<T>(this IEnumerable<T> x, IEnumerable<T> y)
    {
        if (x.Count() != y.Count()) return false;

        bool equals = false;

        foreach (var a in x)
        {
            foreach (var b in y)
            {
                if (a.Equals(b))
                {
                    equals = true;
                    break;
                }
            }

            if (!equals)
            {
                return false;
            }

            equals = false;
        }

        return true;
    }
}
相关问题