2011-08-19 58 views
0

我必须测试树的相等性。换句话说,包含List<T>与孩子和孩子的对象也包含List<T>与孩子等。MSUnit:Assert.AreEqual失败树

我发现你可以使用CollectionAssert测试列表,但是它不能很好地与复合材料一起工作。

有什么建议吗? 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

你有没有超载.Equals操作?如果不是,它将只是检查对象身份。如果有,请告诉我们。 –

+0

添加了.Equals impl –

回答

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; 

试剂不可能匹配,它们是不同的列表的对象。列表<>不覆盖等于,虽然它不清楚你使用的实际类型。写一个辅助函数,其中包含两个试剂列表并检查相等性。您通常会开始比较列表<> .Count,然后逐个处理元素。

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; 
    } 
}