[英]Cannot CollectionAssert equal a class which inherits ICollection
I have two objects of class PCBDataHelper (code below) which I would like to assert are equal as part of a suite of unit tests. 我想断言PCBDataHelper类的两个对象(下面的代码)作为一组单元测试的一部分是相等的。 However, I am unable to get the tests to pass. 但是,我无法通过测试。
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
}
When I use Assert.AreEqual(obj1, obj2)
I get the following output: 当我使用Assert.AreEqual(obj1, obj2)
我得到以下输出:
Assert.AreEqual failed. Assert.AreEqual失败。 Expected:<...HADataFile+PCBDataHelper>. 预计:<... HADataFile + PCBDataHelper>。 Actual:<....HADataFile+PCBDataHelper>. 实际:<.... HADataFile + PCBDataHelper>。
Which leads me to believe it is a collection. 这使我相信它是一个集合。 However when run as CollectionAssert.AreEqual(obj1, obj2)
I get the following pre-compile error: 但是,以CollectionAssert.AreEqual(obj1, obj2)
运行时CollectionAssert.AreEqual(obj1, obj2)
出现以下预编译错误:
cannot convert from '...HADataFile.PCBDataHelper' to 'System.Collections.ICollection' 无法从“ ... HADataFile.PCBDataHelper”转换为“ System.Collections.ICollection”
for both arguments. 对于两个参数。
ICollection<T>
does not implement ICollection
, so if you want to use CollectionAssert.AreEqual
then your class needs ICollection
too: 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.