[英]Cannot CollectionAssert equal a class which inherits ICollection
我想断言PCBDataHelper类的两个对象(下面的代码)作为一组单元测试的一部分是相等的。 但是,我无法通过测试。
public class PCBDataHelper : ICollection<string>
{
//private List<string> pcbData = new List<string>();
private readonly HashSet<string> pcbData = new HashSet<string>();
private const int MaxItemLength = 16;
private const int MaxNumberItems = 5;
#region ICollection<string> Members
public void Add(string item)
{
if (pcbData.Count == MaxNumberItems)
throw new AIException("Max number of items reached. Cannot add item");
if (string.IsNullOrWhiteSpace(item) || item.Length > MaxItemLength)
throw new ArgumentException("PCBData item must not be null and have max length 16!");
if(!pcbData.Add(item))
throw new AIException("Cannot add item, it may already exist!");
}
public void Clear()
{
pcbData.Clear();
}
public bool Contains(string item)
{
return pcbData.Contains(item);
}
public void CopyTo(string[] array, int arrayIndex)
{
pcbData.CopyTo(array, arrayIndex);
}
public int Count
{
get { return pcbData.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(string item)
{
return pcbData.Remove(item);
}
#endregion
#region IEnumerable<string> Members
public IEnumerator<string> GetEnumerator()
{
return pcbData.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return pcbData.GetEnumerator();
}
#endregion
}
当我使用Assert.AreEqual(obj1, obj2)
我得到以下输出:
Assert.AreEqual失败。 预计:<... HADataFile + PCBDataHelper>。 实际:<.... HADataFile + PCBDataHelper>。
这使我相信它是一个集合。 但是,以CollectionAssert.AreEqual(obj1, obj2)
运行时CollectionAssert.AreEqual(obj1, obj2)
出现以下预编译错误:
无法从“ ... HADataFile.PCBDataHelper”转换为“ System.Collections.ICollection”
对于两个参数。
ICollection<T>
不实现ICollection
,因此,如果您想使用CollectionAssert.AreEqual
那么您的类也需要ICollection
:
public class PCBDataHelper : ICollection<string>, ICollection
{
// snip - you need to implement the relevant parts here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.